Skip to content

Instantly share code, notes, and snippets.

@anjelo
Created June 8, 2016 21:51
Show Gist options
  • Save anjelo/68513eb2726a89324d532262aa76e128 to your computer and use it in GitHub Desktop.
Save anjelo/68513eb2726a89324d532262aa76e128 to your computer and use it in GitHub Desktop.
Get distance between 2 coordinates
// cos(d) = sin(φА)·sin(φB) + cos(φА)·cos(φB)·cos(λА − λB),
// where φА, φB are latitudes and λА, λB are longitudes
// Distance = d * R
//taken from http://stackoverflow.com/a/6545031
public static double DistanceBetweenPlaces(double lon1, double lat1, double lon2, double lat2)
{
double R = 6371; // km
double sLat1 = Math.Sin(Radians(lat1));
double sLat2 = Math.Sin(Radians(lat2));
double cLat1 = Math.Cos(Radians(lat1));
double cLat2 = Math.Cos(Radians(lat2));
double cLon = Math.Cos(Radians(lon1) - Radians(lon2));
double cosD = sLat1*sLat2 + cLat1*cLat2*cLon;
double d = Math.Acos(cosD);
double dist = R * d;
return dist;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment