Skip to content

Instantly share code, notes, and snippets.

@fabrizim
Created May 13, 2013 02:36
Show Gist options
  • Save fabrizim/5565861 to your computer and use it in GitHub Desktop.
Save fabrizim/5565861 to your computer and use it in GitHub Desktop.
Haversine Algorithm in Javascript
function haversine_distance(lat1,lat2,R){
R = R || 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var lat1 = lat1.toRad();
var lat2 = lat2.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment