How can I calculate the route distance between two coordinates in MKMapView? I'm not asking for the straight line distance but the distance for a route with turns.
-
2How is your question any different from the two you linked? – esqew May 09 '14 at 19:11
-
1No, it's not a duplicate of that question @tympaniplayer. He/she's not asking for the distance between two points, but the distance of the route. – Lyndsey Scott May 09 '14 at 19:16
-
And user3621544, I know I've used this function before in my code... just trying to find it now. – Lyndsey Scott May 09 '14 at 19:19
-
I'd appropriate that @LyndseyScott – Esra Karakecili May 09 '14 at 19:28
2 Answers
I'm assuming you're using an MKDirectionsRequest
to get a MKDirectionsResponse
from which you're getting your route. For example:
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
// source and destination are the relevant MKMapItem's
request.source = source;
request.destination = destination;
// Specify the transportation type
request.transportType = MKDirectionsTransportTypeAutomobile;
// If you're open to getting more than one route, requestsAlternateRoutes = YES; else requestsAlternateRoutes = NO;
request.requestsAlternateRoutes = YES;
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (!error) {
self.directionsResponse = response;
}
}];
Once you get the MKDirectionsResponse
(in this case self.directionsResponse
) and decide on a specific route index from that response, the CLLocationDistance
of that route (measured in meters) can be found using:
MKRoute *route = self.directionsResponse.routes[currentRoute];
CLLocationDistance distance = route.distance;
And if you don't know which specific route you want to use -- ex. if you want to decide on a route based on the distance -- you can go through the directionsResponse.route
array with a loop to get all the route distances.
Edit: Furthermore, if you want to find the distance in time (measured in seconds), you can do so using:
NSTimeInterval seconds = route.expectedTravelTime;
And in Swift:
let request:MKDirectionsRequest = MKDirectionsRequest()
// source and destination are the relevant MKMapItems
request.setSource(source)
request.setDestination(destination)
// Specify the transportation type
request.transportType = MKDirectionsTransportType.Automobile;
// If you're open to getting more than one route,
// requestsAlternateRoutes = true; else requestsAlternateRoutes = false;
request.requestsAlternateRoutes = true
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler ({
(response: MKDirectionsResponse?, error: NSError?) in
if error == nil {
self.directionsResponse = response
}
})
To get the distance:
let route = directionsResponse.routes[currentRoute] as MKRoute
let distance = route.distance
To get the expected travel time:
let seconds = route.expectedTravelTime

- 37,080
- 10
- 92
- 128
-
@LyndseyScott i followed the same but got error 400 as a response..please have a look http://stackoverflow.com/questions/30775154/error-400-while-adding-route-on-mkmapview – Jun 11 '15 at 09:26
iOS 7 has introduced direction based API known as MKDirections API this helps with root based direction data by which you can play in your application.Refer to below link for more documentation on MKDirection API

- 695
- 6
- 13