简体   繁体   English

打开路线功能无法以编程方式打开苹果地图

[英]Open directions func not opening apple maps programmatically

I am working on a project right now and am stuck on the final part. 我现在正在从事一个项目,并停留在最后一部分。 I have designed an app with 20 custom annotations to be used by our wedding guests to find nearby restaurants, bars, coffee shops, etc. There are various custom annotation to identify what each location is. 我设计了一个带有20个自定义注释的应用,供我们的婚礼客人用来查找附近的餐馆,酒吧,咖啡店等。这里有各种自定义注释,用于标识每个位置。 It also asks for users current location so they can find things nearby. 它还会询问用户当前位置,以便他们可以在附近找到东西。 I would like to be able to click on the annotations and have our friends and family be able to get directions from their current location to the annotation. 我希望能够单击注释,并使我们的朋友和家人能够获取从当前位置到注释的路线。 It seems like the best way to do this is by opening up the apple maps app with my latitude and longitude coordinates in my custom point annotations. 似乎最好的方法是通过在自定义点注释中使用纬度和经度坐标打开Apple Maps应用程序。 My code though does not seem to open that maps app at all. 我的代码虽然似乎根本没有打开该地图应用程序。 I have added a right call out, I have looked through every since question old and new asking about this and can not find a solution. 我添加了一个正确的通知,我浏览了所有新老问题以来一直在寻找的问题,并且找不到解决方案。 Any advice will be much appreciated as this is my first real swift app. 任何建议将不胜感激,因为这是我第一个真正的快速应用程序。 Below is my current open directions function. 以下是我当前的开放路线功能。 Thank you in advance for any help or insight. 预先感谢您的帮助或见解。

    func openDirections(_ address :String?) {

    self.geocoder.geocodeAddressString(address!, completionHandler: { (placemarks :[CLPlacemark]?, error :NSError?) -> Void in

        let placemark = placemarks![0] as CLPlacemark

        let destinationPlacemark = MKPlacemark(coordinate: placemark.location!.coordinate, addressDictionary: placemark.addressDictionary as? [String:NSObject])

        let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: placemark.location!.coordinate, addressDictionary:nil))
        mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])

        let startingMapItem = MKMapItem.forCurrentLocation()
        let destinationMapItem = MKMapItem(placemark: destinationPlacemark)

        let directionsRequest = MKDirectionsRequest()
        directionsRequest.transportType = .automobile
        directionsRequest.source = startingMapItem
        directionsRequest.destination = destinationMapItem

        let directions = MKDirections(request: directionsRequest)

        directions.calculate(completionHandler: { (response :MKDirectionsResponse?, error :NSError?) -> Void in

            let route = response!.routes[0] as MKRoute

            if route.steps.count > 0 {

                for step in route.steps {

                    print(step.instructions)

                }
            }

            self.mapView!.add((route.polyline), level: MKOverlayLevel.aboveRoads)
            } as! MKDirectionsHandler)
        } as! CLGeocodeCompletionHandler)
}

Go ahead and delete everything you wrote and do some research on how to write a new, better, cleaner function. 继续删除您编写的所有内容,并对如何编写新的,更好的,更简洁的功能进行一些研究。 Here is what worked for me and I hope it helps someone else. 这是对我有用的东西,希望对其他人有所帮助。

    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]
    self.title = title
    mapItem.name = title
    mapItem.openInMaps(launchOptions: launchOptions)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM