Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从应用打开地图时,将返回Nil作为自定义点批注的位置标题_Ios_Swift_Xcode_Mapkit - Fatal编程技术网

Ios 从应用打开地图时,将返回Nil作为自定义点批注的位置标题

Ios 从应用打开地图时,将返回Nil作为自定义点批注的位置标题,ios,swift,xcode,mapkit,Ios,Swift,Xcode,Mapkit,我正在开发一个应用程序,它有一个列表,列出了我和我的伴侣的婚礼要去的地方的多个自定义点注释。我最近想出了如何创建一个函数来打开地图。问题是,单击详图索引时,“方向”标题显示“未知位置”或“零”。我为所有自定义点注释设置了一个标题集,我只是在地图上查找单击注释的标题作为最终目的地标题。非常感谢您的帮助。这是我的第一个完整应用程序,这是在启动和运行它之前最不需要弄清楚的事情。提前感谢你的帮助。如果问题不是来自此函数,则很乐意提供更多代码 // Open maps app programati

我正在开发一个应用程序,它有一个列表,列出了我和我的伴侣的婚礼要去的地方的多个自定义点注释。我最近想出了如何创建一个函数来打开地图。问题是,单击详图索引时,“方向”标题显示“未知位置”或“零”。我为所有自定义点注释设置了一个标题集,我只是在地图上查找单击注释的标题作为最终目的地标题。非常感谢您的帮助。这是我的第一个完整应用程序,这是在启动和运行它之前最不需要弄清楚的事情。提前感谢你的帮助。如果问题不是来自此函数,则很乐意提供更多代码

    // 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)
}

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

    // 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)
        }
    }
}
相似!