iOS MKDirections for multiple destinations and a single source

僤鯓⒐⒋嵵緔 提交于 2019-12-03 21:43:17

As far as I'm aware there is no way to specify more than two points for an MKDirections request. Your proposed solution of creating 10 separate requests should work in theory, though keep in mind that:

  1. MKDirections does not calculate directions locally on the device, but reaches out to a remote API which calculates them and returns the response. If you're calculating 10 routes, there will likely be a noticeable delay for the user while waiting for all the results to be returned.

  2. Because directions are calculated using a remote API, Apple likely would frown on something like this- if you're hitting their directions API too frequently they might throttle you, or they may just reject the app when it's submitted to the App Store if they see that this is happening.

I'm not sure of your exact requirements, but depending on the accuracy you need, one solution could be to calculate the distance between the start point and each end point using the haversine formula, which would give you the distance between the two points as-the-crow-flies. Because you're calculating walking directions (as opposed to driving, where you're limited to following specific roads and subject to traffic), the closest distance from these calculations will likely be the closest in terms of actual walking distance/time, at which point you could then make a single MKDirections request for those details.

// try this annotation.coordinate is the locations and ULManager.location //as the user location

        let dirRe = MKDirectionsRequest()
        dirRe.source = MKMapItem(placemark: MKPlacemark(coordinate:          (self.ULManager.location?.coordinate)!, addressDictionary: nil))
        dirRe.destination = MKMapItem(placemark: MKPlacemark(coordinate: (annotation.coordinate), addressDictionary: nil))
        dirRe.transportType = .Walking
        let requestAll = MKDirections(request: dirRe)

        requestAll.calculateDirectionsWithCompletionHandler ({
            (response: MKDirectionsResponse?, error: NSError?) in
               // do your staff here
        })
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!