0

I have a MKMapview with multiple annotations. When the user chooses an annotation, the "callout" view shows the location's name and the distance between the location and the user location.

I'm stuck at getting the distance. Prior to iOS 5, I could use Google direction API to get the distance, but now I can't. Is there any alternative solution?

Thank you!

EDIT:

The distance I refer is the distance of the route between two locations. The route has many routes. It's not the distance of the straight line as the result of "distanceFromLocation:"

vietstone
  • 8,784
  • 16
  • 52
  • 79

2 Answers2

4

If you have two CLLocations, you can use the distanceFromLocation: method to get the distance in meters between them.

Example:

CLLocation* first = [[CLLocation alloc] initWithLatitude:firstLatitude longitude:firstLongitude];
CLLocation* second = [[CLLocation alloc] initWithLatitude:secondLatitude longitude:secondLongitude];

CGFloat distance = [first distanceFromLocation:second];

You can get the latitude and longitude of your annotation (not your annotation view) from its coordinate or similar property on your annotation class.

If you have a list of annotations and want to get the distance of a path between them, simply:

CGFloat distance = 0;
for(int idx = 0; idx < [mapView.annotations count] - 1; ++idx) {
    CLLocationCoordinate2D firstCoord = [mapView.annotations[idx] coordinate];
    CLLocationCoordinate2D secondCoord = [mapView.annotations[idx + 1] coordinate];

    CLLocation* first = [[CLLocation alloc] initWithLatitude:firstCoord.latitude longitude:secondCoord.latitude];
    CLLocation* second = [[CLLocation alloc] initWithLatitude:secondCoord.latitude longitude:secondCoord.longitude];

    distance += [first distanceFromLocation:second];
}
eric.mitchell
  • 8,817
  • 12
  • 54
  • 92
  • Hi, I know distanceFromLocation, but it's only the distance of the straight line between two locations. the distance I refer is the distance of the route, the route has many turns – vietstone Nov 27 '12 at 17:18
  • Simple. All you need is each point in the route, and iterate through them similarly to how I did in the code I provided. Does that make sense? – eric.mitchell Nov 27 '12 at 17:20
  • But the way of getting all point in the route is Google direction api. Do you have any other solution? I really need the alternative solution. Thank you – vietstone Nov 27 '12 at 17:24
  • I see. So you don't have the points contained in the route, only the start and end point. Have you considered simply opening the Maps app? Also, see this question: http://stackoverflow.com/questions/4609340/generate-walking-directions-with-maps-app-ios – eric.mitchell Nov 27 '12 at 17:37
  • Hi, I know the built-in Maps app. It draw routes between two locations. But my need is a map with multiple locations, each location show the distance from user location, please see my question – vietstone Nov 27 '12 at 17:41
  • Why can you no longer use the Google Directions API? – eric.mitchell Nov 28 '12 at 16:14
  • What if I want to calculate using polyline/cordinates between two locations. I have successfully drawn a route now I want to calculate the distance along the rote – nr5 Dec 17 '12 at 05:29
  • Simply iterate through the map's overlays (like in my example, just change annotations to overlays) and sum together the distance between each overlay – eric.mitchell Dec 17 '12 at 15:29
  • It's should be: CLLocation* first = [[CLLocation alloc] initWithLatitude:firstCoord.latitude longitude:secondCoord.longitude] – Edward Chiang Oct 07 '14 at 03:08
  • @iOS Now (iOS 7 and later) you can get route by MKDirections, MKDirectionsRequest and MKDirectionsResponse. The route information is MKRoute (there's a distance property). A starting point: http://technet.weblineindia.com/mobile/draw-route-between-2-points-on-map-with-ios7-mapkit-api/ – vietstone Feb 27 '15 at 03:18
2

Now (iOS 7 and later) you can get a route with MKDirections, MKDirectionsRequest and MKDirectionsResponse.

The route information is MKRoute (there's a distance property).

For an example follow this link.

real_ate
  • 10,861
  • 3
  • 27
  • 48
vietstone
  • 8,784
  • 16
  • 52
  • 79