0

Am trying to calculate distance from one point to another. I am able to find method for that.

CLLocationDistance dist = [loc distanceFromLocation:loc2];

But what it does is calculate distance in a straight line.

What I want is to calculate distance by roadways.

For Example: Lets say If i give from address as Home and To address as Work. I want the distance between Home to Work by roadways. By roadways I get 750meters. But if i calculate by the method distanceFromLocation I get only 400meters. Can someone help me with an example to calculate distance.

Jaff
  • 105
  • 1
  • 17
  • Please See This Example https://www.shinobicontrols.com/blog/ios7-day-by-day-day-13-route-directions-with-mapkit – Rajesh May 31 '16 at 05:59
  • best Easy Example http://www.devfright.com/mkdirections-tutorial/ – Rajesh May 31 '16 at 06:00
  • Thanks Ill check right now – Jaff May 31 '16 at 06:02
  • I think these are the things I want MKDirections and MKDirectionsRequest. I tried using the calculateDirectionWithCompletionHandler. My response getting error. – Jaff May 31 '16 at 06:54
  • Check this post http://stackoverflow.com/questions/35625950/estimated-time-between-two-locations-in-ios/35628434#35628434 – kb920 Jun 02 '16 at 05:27

1 Answers1

0

Check out this answer :

CLLocationCoordinate2D pointACoordinate = [pointAAnnotation coordinate];
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];  

CLLocationCoordinate2D pointBCoordinate = [pointBAnnotation coordinate];
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];  

float distanceMeters = [pointALocation distanceFromLocation:pointBLocation];
float distanceMiles = (distanceMeters / 1609.344);   

Here pointAAnnotation & pointBAnnotation are the two Annotations which you need to calculate distance between them.

Ref : Distance bw two Latitude & Longitude

Community
  • 1
  • 1
Balaji Ramakrishnan
  • 1,909
  • 11
  • 22
  • Thanks for the reply. I want to calculate distance between two location by road ways. The distanceFromLocation: will draw a straight path between two locations and give that distance right? Lets say If i give from address as Home and to address as work. I want the distance between from Home to Work by roadways. By roadways I get 750meters. But if i calculate distanceFromLocation I get only 400meters. – Jaff May 31 '16 at 06:26
  • Yes.. this will return direct Straight line length. Better you try with google Api to get exact Distance – Balaji Ramakrishnan May 31 '16 at 06:58