ios - 为什么我的按钮无法从应用程序推送到 Apple map ?

标签 ios swift xcode button mapkit

我正在开发一个导航应用程序,其工作原理类似于普通 map 应用程序,允许您通过搜索表等搜索位置。在该应用程序中,用户可以通过单击他们想要的可搜索结果来创建注释.

问题:当用户点击注释时,其上方应出现一个弹出按钮,因为我已以编程方式创建了它。单击后,它应该将用户推送到 Apple map 应用程序,并包含他们单击的预加载注释的路线。它不再将我推送到那里,这很奇怪,因为它曾经有效。这是以编程方式创建的按钮:

extension ViewController : MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }
        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.pinTintColor = UIColor.red
        pinView?.canShowCallout = true
        let smallSquare = CGSize(width: 30, height: 30)
        let button = UIButton(frame: CGRect(origin: (CGPoint()), size: smallSquare))
        button.setBackgroundImage(UIImage(named: "car"), for: [])
        button.addTarget(self, action: #selector(getDirections), for: UIControlEvents.touchUpInside)
        
        pinView?.leftCalloutAccessoryView = button
        return pinView
    }
}

这应该(在类里面)执行

@objc func getDirections(){
    if let selectedPin = selectedPin {
        let mapItem = MKMapItem(placemark: selectedPin)
        let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
        mapItem.openInMaps(launchOptions: launchOptions)
    }
}

Objective-C 函数会不会有什么奇怪的地方?这是我最近添加的代码,可能会导致该问题,但我真的不知道:

func resolveAddress(for averageCoordinate: CLLocationCoordinate2D, completion: @escaping (MKPlacemark?) -> () ) {

    let geocoder = CLGeocoder()
    let averageLocation = CLLocation(latitude: averageCoordinate.latitude, longitude: averageCoordinate.longitude)
    geocoder.reverseGeocodeLocation(averageLocation) { (placemarks, error) in
        guard error == nil,
            let placemark = placemarks?.first
        else {
            completion(nil)
            return
        }
        completion(MKPlacemark(placemark: placemark ))
    }
}

@IBAction func middleFinderButton(_ sender: Any) {
    
    let totalLatitude = mapView.annotations.reduce(0) { $0 + $1.coordinate.latitude }
    
    let totalLongitude = mapView.annotations.reduce(0) { $0 + $1.coordinate.longitude }
    
    let averageLatitude = totalLatitude/Double(mapView.annotations.count)
    
    let averageLongitude = totalLongitude/Double(mapView.annotations.count)
    
    let centerPoint = MKPointAnnotation()
    
    centerPoint.coordinate.latitude = averageLatitude
    centerPoint.coordinate.longitude = averageLongitude
    
    
    mapView.addAnnotation(centerPoint)
    
    resolveAddress(for: centerPoint.coordinate) { placemark in
        if let placemark = placemark {
            self.mapView.addAnnotation(placemark)
        } else {
            self.mapView.addAnnotation(centerPoint)
        }
    }

    print(totalLatitude)
    print(totalLongitude)
    print(averageLatitude)
    print(averageLongitude)
    print(centerPoint.coordinate)
}

github链接:github.com/derekvandermark/VHNavigationAppOfficial-v-0.93

有人知道为什么这个按钮不起作用并将应用程序推送到具有预设路线的 map 吗?会不会是模拟器的问题? (它曾经有效,所以我怀疑......)

最佳答案

要将用户推送到 map 应用程序,您可以尝试以下操作:

func openMap(lat: String, lng: String){

    let lat1 : NSString = lat as NSString
    let lng1 : NSString = lng as NSString

    let latitude:CLLocationDegrees =  lat1.doubleValue
    let longitude:CLLocationDegrees =  lng1.doubleValue

    let regionDistance:CLLocationDistance = 10000
    let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
    let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
    let options = [
        MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
        MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
    ]
    let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
    let mapItem = MKMapItem(placemark: placemark)
    mapItem.openInMaps(launchOptions: options)
}

关于ios - 为什么我的按钮无法从应用程序推送到 Apple map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52825201/

相关文章:

ios - UICollectionview 重新排序图像单元格

ios - 如何在 IOS/SWIFT/OBJ-C/CORDOVa 上获取 Finger iD(哪个手指用于触摸 ID)或唯一触摸 ID

ios - 在 UICollectionView 中更改滚动方向

ios - Collection View cellForItemAt indexPath 未被调用(Swift)

ios - 提供的 Assets 名称无效 : '(null)'

xcode - Swift 中递归引用自身的函数

ios - Cocos2d 和 SpriteBuilder 中的 Admob

iphone - 接近应用程序版本 : 2 Quick Questions

swift - 如何重构我的代码以避免重复

ios - Xcode 错误 "Profile doesn' t include the beta-reports-active entitlement"for adhoc profile