Created
June 28, 2013 20:38
-
-
Save thiagoperes/5887917 to your computer and use it in GitHub Desktop.
Helper functions to calculate the distance between two coordinates and to create a distance string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLLocationDistance distanceBetweenCoordinates(CLLocationDegrees startLat, CLLocationDegrees startLng, | |
CLLocationDegrees endLat, CLLocationDegrees endLng) | |
{ | |
CLLocation *locA = [[CLLocation alloc] initWithLatitude:startLat | |
longitude:startLng]; | |
CLLocation *locB = [[CLLocation alloc] initWithLatitude:endLat | |
longitude:endLng]; | |
return [locA distanceFromLocation:locB]; | |
} | |
NSString* distanceString(CLLocationDistance distance) | |
{ | |
NSString *distanceString; | |
if (distance >= 1000) | |
{ | |
distanceString = [NSString stringWithFormat:@"%.1lf km", distance/1000.0f]; | |
} | |
else | |
{ | |
distanceString = [NSString stringWithFormat:@"%d m", (int)distance]; | |
} | |
return distanceString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment