-1

I am making an app based on google map.where I am measuring distance between two places. so I have tried to find distance between two place coordinate by predefined method "getDistanceFrom" and tried with googleDirectionAPi. if I use predefine method I did not get correct distance. if I use googleDirectionApi sometimes I did not get response.

so please suggest me solution where I can find response on every hit of api and will able to get distance between two place?

Joe Sebin
  • 452
  • 4
  • 24
manoj
  • 9
  • 5
  • Can you add an example that includes the types involved? Also, have a look at [this](https://stackoverflow.com/questions/11077425/finding-distance-between-cllocationcoordinate2d-points) and [this](https://stackoverflow.com/questions/3905896/distancefromlocation-calculate-distance-between-two-points) – Toine Heuvelmans Jun 07 '18 at 11:20
  • Do you want the distance in a straight line or the driving/travelling distance? – Fogmeister Jun 07 '18 at 11:34
  • Their are Two types of Distance Linear Distance using Formula of Latlong and Path Distance. Linear is Free...Path is chargable from Google API. – Avinash Verma Jun 07 '18 at 11:35
  • @ToineHeuvelmans thanks for reply – manoj Jun 07 '18 at 11:39

3 Answers3

1

You can very easily get the distance between two locations using CoreLocation.

The documentation for CLLocation shows this... https://developer.apple.com/documentation/corelocation/cllocation#

You can create a CLLocation like...

CLLocation* location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1]

Once you have two locations you can find the distance with...

CLLocationDistance distance = [location1 distanceFromLocation: location2];

Note, the documentation shows that getDistanceFrom was deprecated in iOS3.2 (about 8 years ago) so don't use that.

(Forgive any errors in syntax, it has been a few years since I wrote any Objective-C) :D

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Thanks, but it will provide direct distance between two places. i am using GoogleDirectionApi for correct data of distance where sometimes i am not getting distance or invalid request – manoj Jun 08 '18 at 12:47
  • @manoj then I suggest answering the comments under your question. :-) – Fogmeister Jun 08 '18 at 14:13
0

You need to use these coordinates to get the CLLocations for the respective coordinates using the following line of code

CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
NSLog(@"Distance i meters: %f", [location1 distanceFromLocation:location2]);
Sargis
  • 1,196
  • 1
  • 18
  • 31
-1

You need to make a HTTP request, call the folowing URL, and it will return the distance between two points.

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=YOUR_API_KEY

To get the API Key go to the google developer console.

Nick
  • 875
  • 6
  • 20
  • thanks for reply, but it is not providing data sometime, i have tried with full address of source and destination coordinate and name – manoj Jun 08 '18 at 12:48