Skip to content

Instantly share code, notes, and snippets.

@bretdavidson
Created April 30, 2014 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bretdavidson/ede841a1c3ff2349a9c5 to your computer and use it in GitHub Desktop.
Save bretdavidson/ede841a1c3ff2349a9c5 to your computer and use it in GitHub Desktop.
var Distance = {
rad: function (x) {
return x * Math.PI / 180;
},
haversine: function (p1, p2) {
var R = 637100000,
dLat = this.rad(p2.latitude - p1.latitude),
dLong = this.rad(p2.longitude - p1.longitude),
a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(this.rad(p1.latitude)) * Math.cos(this.rad(p2.latitude)) * Math.sin(dLong / 2) * Math.sin(dLong / 2),
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)),
d = R * c;
return Math.round(d);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment