Created
October 18, 2014 21:55
-
-
Save elbuo8/849464e6d98009f2699d to your computer and use it in GitHub Desktop.
loll
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 calculateDistance(lat1, lon1, lat2, lon2) { | |
var R = 6371; // km | |
var dLat = (lat2 - lat1).toRad(); | |
var dLon = (lon2 - lon1).toRad(); | |
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + | |
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * | |
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; | |
} | |
Number.prototype.toRad = function() { | |
return this * Math.PI / 180; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment