Skip to content

Instantly share code, notes, and snippets.

@jinahadam
Created October 8, 2013 05:50
Show Gist options
  • Save jinahadam/6880085 to your computer and use it in GitHub Desktop.
Save jinahadam/6880085 to your computer and use it in GitHub Desktop.
Distance betweeen two gps points Haversine a = sin²(Δφ/2) + cos(φ1).cos(φ2).sin²(Δλ/2)
var 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