Skip to content

Instantly share code, notes, and snippets.

@pherrymason
Created October 30, 2013 21:15
Show Gist options
  • Save pherrymason/7240409 to your computer and use it in GitHub Desktop.
Save pherrymason/7240409 to your computer and use it in GitHub Desktop.
Distance Between Points
// 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