Skip to content

Instantly share code, notes, and snippets.

@kucukkanat
Last active January 27, 2018 22:21
Show Gist options
  • Save kucukkanat/1fd8796e3dabd38db90c to your computer and use it in GitHub Desktop.
Save kucukkanat/1fd8796e3dabd38db90c to your computer and use it in GitHub Desktop.
Distance Finder
var getDistance = function(lat1,lon1,lat2,lon2){
var R = 6371; // km
//has a problem with the .toRad() method below.
var dLat = toRad(lat2-lat1);
var dLon = toRad(lon2-lon1);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d * 1000; //Return as meters
}
function toRad(e) {
return e * Math.PI / 180;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment