iOS MKDirections for multiple destinations and a single source

别说谁变了你拦得住时间么 提交于 2019-12-21 06:45:30

问题


My application needs to get the "closest" walking point to the user from a set of 10. I have seen that MKDirections does this with "calculateDirectionsWithCompletionHandler" for a single source, destination locations. My question is, do I have do make 10 requests and then check which one is the closest one in distance or there is a way of sending multiple destinations and the response would be the closest one walking?

Thank you


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/23104337/ios-mkdirections-for-multiple-destinations-and-a-single-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!