Skip to content

Instantly share code, notes, and snippets.

@fsschmitt
Created August 5, 2016 15:32
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 fsschmitt/72defbbce66db86d4424db0e96b5e735 to your computer and use it in GitHub Desktop.
Save fsschmitt/72defbbce66db86d4424db0e96b5e735 to your computer and use it in GitHub Desktop.
var distance = function(lat1, lon1, lat2, lon2){
var R = 6371e3; // metres
var φ1 = lat1 * Math.PI / 180;
var φ2 = lat2 * Math.PI / 180;
var Δφ = (lat2-lat1)* Math.PI / 180;
var Δλ = (lon2-lon1)* Math.PI / 180;
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
//distance in meters
var d = R * c;
return d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment