Created
October 30, 2013 21:15
-
-
Save pherrymason/7240409 to your computer and use it in GitHub Desktop.
Distance Between 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
// Manual: Haversine Formula: shortest route over a sphere | |
function distance(latlng1, latlng2) { | |
var R = 6371, // Radius of the earth, in km. Need miles? change to 3961 | |
dLat = (latlng2.lat - latlng1.lat) * (Math.PI/180), | |
dLon = (latlng2.lng - latlng1.lng) * (Math.PI/180), | |
a = Math.sin(dLat/2)*Math.sin(dLat/2) + | |
Math.cos(latlng1.lat * (Math.PI/180)) * | |
Math.cos(latlng.lat * (Math.PI/180)) * | |
Math.sin(dLon/2) * Math.sin(dLon/2), | |
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)), | |
d = R * c; | |
return d; | |
} | |
// Google Maps v3 with Geometry Library | |
// @see https://developers.google.com/maps/documentation/javascript/geometry | |
google.maps.geometry.spherical.computeDistanceBetween( latLngA, latLngB ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment