Created
May 18, 2015 13:26
-
-
Save mrtnbroder/5dde44c4fdc7690a60da to your computer and use it in GitHub Desktop.
get distance between two coordinates
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
function distance(obj) { | |
var R = 6371; // km | |
var dLat = (obj.lat2 - obj.lat1) * Math.PI / 180; | |
var dLon = (obj.lon2 - obj.lon1) * Math.PI / 180; | |
var lat1 = obj.lat1 * Math.PI / 180; | |
var lat2 = obj.lat2 * Math.PI / 180; | |
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + | |
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2); | |
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
var km = R * c; | |
var mi = km * 0.621371; | |
return { | |
km: km, | |
mi: mi | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment