15

how to calculate distance between two location in miles using distanceFromLocation method of mapkit framework.

Thanks

Ayesha Fatima
  • 359
  • 2
  • 4
  • 16

3 Answers3

28

This has already been answered in metric here. Now you just need to convert meters to miles which is:

1 Meter = 0.000621371192 Miles 

or

1 Mile = 1609.344 Meters 
Community
  • 1
  • 1
Daniel Pereira
  • 1,785
  • 12
  • 10
26

Try below code:

double latA = [currentlat floatValue];
double logA = [currentlong floatValue];

double latB = [toLat floatValue];
double logB = [toLong floatValue];

CLLocation *locA = [[CLLocation alloc] initWithLatitude:latA
                                              longitude:logA];

CLLocation *locB = [[CLLocation alloc] initWithLatitude:latB longitude:logB];
CLLocationDistance distance = [locA distanceFromLocation:locB];
NSLog(@"Calculated Miles %@", [NSString stringWithFormat:@"%.1fmi",(distance/1609.344)]);
Mohammed Ebrahim
  • 849
  • 1
  • 12
  • 22
3

The function seems fairly self explanatory in Apple's documentation? It will give the distance between the user's location and the location given, so you'd have to create a new CLLocation object to tell the iPhone where you want to go.

The function gives the result in meters. To convert to miles, multiply by 0.000621371192. More information is given in this thread.

Community
  • 1
  • 1
semisight
  • 914
  • 1
  • 8
  • 15