how to calculate distance between two location in miles using distanceFromLocation method of mapkit framework.
Thanks
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
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)]);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With