OGeek|极客世界-中国程序员成长平台

标题: ios - 从 App 打开 map 返回 Nil 作为自定义点注释的位置标题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:25
标题: ios - 从 App 打开 map 返回 Nil 作为自定义点注释的位置标题

我正在开发一个应用程序,该应用程序包含多个自定义点注释列表,用于为我的伴侣和我的婚礼参观地点。我最近想出了如何创建一个函数来打开该 map 。问题是,当您单击标注时,路线标题显示未知位置或无。我为所有自定义点注释设置了一个标题,我只是在寻找点击注释的标题作为最终目的地标题在 map 上。任何帮助深表感谢。这是我的第一个完整应用程序,这是在启动和运行之前要弄清楚的最后一件事。在此先感谢您的帮助。如果问题不是来自此函数,很高兴提供更多代码。

    // Open maps app programatically
func mapView(_ mapView: MKMapView, annotationView view:MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    let placemark = MKPlacemark(coordinate: view.annotation!.coordinate, addressDictionary: nil)
    let mapItem = MKMapItem(placemark: placemark)
    let launchOptions = [MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeTransit]
    view.canShowCallout = true
    self.title = title
    mapItem.name = title
    mapItem.openInMaps(launchOptions: launchOptions)
}



Best Answer-推荐答案


所以经过大量研究并试图找出这个问题后,我终于找到了一个解决方案,它是对其他一些问题的几种不同答案的组合。以下是对我有用的解决方案。我希望这对其他人有帮助。

    // Open maps app programatically
func openMapsAppWithDirections(to coordinate: CLLocationCoordinate2D, destinationName name: String) {
    let options = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
    let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = name // Provide the name of the destination in the To: field
    mapItem.openInMaps(launchOptions: options)
}

func mapView(_ MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped Control: UIControl) {

    if Control == annotationView.rightCalloutAccessoryView {
        if let annotation = annotationView.annotation {
            // Unwrap the double-optional annotation.title property or
            // name the destination "Unknown" if the annotation has no title
            let destinationName = (annotation.title ?? nil) ?? "Unknown"
            openMapsAppWithDirections(to: annotation.coordinate, destinationName: destinationName)
        }
    }
}

关于ios - 从 App 打开 map 返回 Nil 作为自定义点注释的位置标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43231620/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4