Skip to content

Instantly share code, notes, and snippets.

@patrickarlt
Created May 31, 2012 15:39
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 patrickarlt/2844227 to your computer and use it in GitHub Desktop.
Save patrickarlt/2844227 to your computer and use it in GitHub Desktop.
Set of functions for determining the distance between 2 lat,lng points
var radiusOfEarth = 6378100.0;
function deg2rad(deg) {
return deg * (Math.PI / 180);
}
function rad2deg(rad) {
return rad * (180 / Math.PI);
}
function computeDistanceBetween(from, to) {
return distance(from.lat, from.lng, to.lat, to.lng);
}
function distance(lat1, lng1, lat2, lng2) {
return ( radiusOfEarth * Math.acos( Math.cos( deg2rad(lat1) ) * Math.cos( deg2rad(lat2) ) * Math.cos( deg2rad(lng2) - deg2rad(lng1) ) + Math.sin( deg2rad(lat1) ) * Math.sin( deg2rad(lat2) ) ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment