Last active
January 10, 2016 14:06
-
-
Save vincentorback/e7252897dd043fc7e434 to your computer and use it in GitHub Desktop.
Get distance between to geolocations in kilometers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var distance = distanceBetween({ | |
lat: 59.329323, | |
lng: 18.068581 | |
}, { | |
lat: 57.708870, | |
lng: 11.974560 | |
}); | |
function getRadius(x) { | |
return x * Math.PI / 180; | |
} | |
function distanceBetween(x, y) { | |
var R = 6371; // Earth radius | |
var dLat = getRadius(y.lat - x.lat); | |
var dLong = getRadius(y.lng - x.lng); | |
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(getRadius(x.lat)) * Math.cos(getRadius(y.lat)) * Math.sin(dLong / 2) * Math.sin(dLong / 2); | |
var b = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
var c = R * b; | |
return Math.round(c) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment