Created
May 31, 2012 15:39
-
-
Save patrickarlt/2844227 to your computer and use it in GitHub Desktop.
Set of functions for determining the distance between 2 lat,lng points
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 radiusOfEarth = 6378100.0; | |
function deg2rad(deg) { | |
return deg * (Math.PI / 180); | |
} | |
function rad2deg(rad) { | |
return rad * (180 / Math.PI); | |
} | |
function computeDistanceBetween(from, to) { | |
return distance(from.lat, from.lng, to.lat, to.lng); | |
} | |
function distance(lat1, lng1, lat2, lng2) { | |
return ( radiusOfEarth * Math.acos( Math.cos( deg2rad(lat1) ) * Math.cos( deg2rad(lat2) ) * Math.cos( deg2rad(lng2) - deg2rad(lng1) ) + Math.sin( deg2rad(lat1) ) * Math.sin( deg2rad(lat2) ) ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment