function haversineDistance(coords1, coords2, isMiles) {
function toRad(x) {
return x * Math.PI / 180;
}
var lon1 = coords1[0];
var lat1 = coords1[1];
var lon2 = coords2[0];
var lat2 = coords2[1];
var R = 6371; // km
var x1 = lat2 - lat1;
var dLat = toRad(x1);
var x2 = lon2 - lon1;
var dLon = toRad(x2)
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;
if(isMiles) d /= 1.60934;
return d;
}
console.log(1000*haversineDistance([-58.401255,-34.640606],[-58.368695,-34.602296],false));
5198.402297945586
Last active
October 28, 2019 15:18
-
-
Save aaizemberg/b9ce3e077492482603e1c2e2f7be10ab to your computer and use it in GitHub Desktop.
haversineDistance en javascript
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment