菜鸟教程小白 发表于 2022-12-11 19:25:53

ios - 从 App 打开 map 返回 Nil 作为自定义点注释的位置标题


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

<pre><code>    // 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 =
    view.canShowCallout = true
    self.title = title
    mapItem.name = title
    mapItem.openInMaps(launchOptions: launchOptions)
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>所以经过大量研究并试图找出这个问题后,我终于找到了一个解决方案,它是对其他一些问题的几种不同答案的组合。以下是对我有用的解决方案。我希望这对其他人有帮助。</p>

<pre><code>    // Open maps app programatically
func openMapsAppWithDirections(to coordinate: CLLocationCoordinate2D, destinationName name: String) {
    let options =
    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 &#34;Unknown&#34; if the annotation has no title
            let destinationName = (annotation.title ?? nil) ?? &#34;Unknown&#34;
            openMapsAppWithDirections(to: annotation.coordinate, destinationName: destinationName)
      }
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 从 App 打开 map 返回 Nil 作为自定义点注释的位置标题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43231620/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43231620/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 从 App 打开 map 返回 Nil 作为自定义点注释的位置标题