Skip to content

Instantly share code, notes, and snippets.

@georgel
Created April 9, 2012 04:18
Show Gist options
  • Save georgel/2341397 to your computer and use it in GitHub Desktop.
Save georgel/2341397 to your computer and use it in GitHub Desktop.
Calculate distance between 2 cllocation
#define DEG_TO_RAD 0.017453292519943295769236907684886
#define EARTH_RADIUS 6372797.560856
// return distance between two lat/lon points, in meters
double DistanceBetweenCoords(double fromLat, double fromLong, double toLat,
double toLong)
{
double latitudeArc = (fromLat - toLat) * DEG_TO_RAD;
double longitudeArc = (fromLong - toLong) * DEG_TO_RAD;
double latitudeH = sin(latitudeArc * 0.5);
latitudeH *= latitudeH;
double lontitudeH = sin(longitudeArc * 0.5);
lontitudeH *= lontitudeH;
double tmp = cos(fromLat*DEG_TO_RAD) * cos(toLat*DEG_TO_RAD);
return 2.0 * asin(sqrt(latitudeH + tmp*lontitudeH)) * EARTH_RADIUS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment