Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the distance between two locations in miles in objective c?

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

Thanks

like image 249
Ayesha Fatima Avatar asked Oct 16 '11 16:10

Ayesha Fatima


3 Answers

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 
like image 196
Daniel Pereira Avatar answered Nov 07 '22 04:11

Daniel Pereira


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)]);
like image 25
Mohammed Ebrahim Avatar answered Nov 07 '22 04:11

Mohammed Ebrahim


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.

like image 29
semisight Avatar answered Nov 07 '22 03:11

semisight