Skip to content

Instantly share code, notes, and snippets.

@tlivings
Created April 8, 2015 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlivings/729d901577957d3da821 to your computer and use it in GitHub Desktop.
Save tlivings/729d901577957d3da821 to your computer and use it in GitHub Desktop.
Distance from CLLocation
//Uses the haversine formula.
-(double) distanceFromCLLocation:(CLLocation*)location {
double result;
double earthRadius = 3959; //miles
double latDelta = (location.coordinate.latitude - self.latitude.doubleValue) * (M_PI/180);
double lonDelta = (location.coordinate.longitude - self.longitude.doubleValue) * (M_PI/180);
double lat1 = self.latitude.doubleValue * (M_PI/180);
double lat2 = location.coordinate.latitude * (M_PI/180);
double a = (sin(latDelta/2) * sin(latDelta/2)) + (sin(lonDelta/2) * sin(lonDelta/2)) * (cos(lat1) * cos(lat2));
double c = 2 * atan2(sqrt(a), sqrt(1-a));
result = earthRadius * c;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment