Skip to content

Instantly share code, notes, and snippets.

@tlhunter
Created May 18, 2017 00:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlhunter/6bd77b58c823bc0019880110271b883a to your computer and use it in GitHub Desktop.
Save tlhunter/6bd77b58c823bc0019880110271b883a to your computer and use it in GitHub Desktop.
Get the distance between two points in meters
function getDistance(p1, p2) {
var radlat1 = Math.PI * p1.lat/180;
var radlat2 = Math.PI * p2.lat/180;
var theta = p1.lng - p2.lng;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist);
dist = dist * 180 / Math.PI;
dist = dist * 60 * 1.1515 * 1.609344 * 1000;
return dist;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment