Skip to content

Instantly share code, notes, and snippets.

@thiagoperes
Created June 28, 2013 20:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thiagoperes/5887917 to your computer and use it in GitHub Desktop.
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
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