Skip to content

Instantly share code, notes, and snippets.

@dhoko
Last active December 3, 2015 23:48
Show Gist options
  • Save dhoko/9524883 to your computer and use it in GitHub Desktop.
Save dhoko/9524883 to your computer and use it in GitHub Desktop.
Calculer la distance en km entre deux points
/**
* Find the distance between two points
* @param {Object} pointA {lat,lng}
* @param {Object} pointB {lat,lng}
* @return {Float} Distance
*/
function howFar(pointA, pointB) {
var R,Dlat,Dlong,a,b,c,d;
var rad = function(x) {return x*Math.PI/180;}
var latB = pointB.lat,
latA = pointA.lat,
lngA = pointA.lng,
lngB = pointB.lng;
R = 6371; // Earth radius
dLat = rad( LatB - LatA );
dLong = rad( LngB - LngA );
a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(rad(LatA)) * Math.cos(rad(LatB)) * Math.sin(dLong/2) * Math.sin(dLong/2);
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
d = R * c;
return d.toFixed(3);
}
@dhoko
Copy link
Author

dhoko commented Mar 13, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment