Skip to content

Instantly share code, notes, and snippets.

@stewones
Created October 26, 2017 14:19
Show Gist options
  • Save stewones/a5671fbc8fc249fe937fa4d9f29b5160 to your computer and use it in GitHub Desktop.
Save stewones/a5671fbc8fc249fe937fa4d9f29b5160 to your computer and use it in GitHub Desktop.
calc distances
public distanceTo = function(endLat, endLon, startLat, startLon){
endLat = Number(endLat);
endLon = Number(endLon);
startLat = Number(startLat);
startLon = Number(startLon);
var R = 6371e3; // metres
var φ1 = startLat * Math.PI / 180;
var φ2 = endLat * Math.PI / 180;
var Δφ = (endLat-startLat) * Math.PI / 180;
var Δλ = (endLon-startLon) * 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));
var d = R * c;
d = d * 0.000621371192; //convert to miles
return d;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment