-
-
Save SafeAF/a9fa009b9aa39ad717f0 to your computer and use it in GitHub Desktop.
Compute the distance between two coordinates
This file contains hidden or 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 computeDistance(startCoords, destCoords) { | |
var startLatRads = degreesToRadians(startCoords.latitude); | |
var startLongRads = degreesToRadians(startCoords.longitude); | |
var destLatRads = degreesToRadians(destCoords.latitude); | |
var destLongRads = degreesToRadians(destCoords.longitude); | |
var Radius = 6371; // radius of the Earth in km | |
var distance = | |
Math.acos(Math.sin(startLatRads) * Math.sin(destLatRads) + | |
Math.cos(startLatRads) * Math.cos(destLatRads) * | |
Math.cos(startLongRads - destLongRads)) * Radius; | |
return distance; | |
} | |
function degreesToRadians(degrees) { | |
var radians = (degrees * Math.PI)/180; | |
return radians; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment