Skip to content

Instantly share code, notes, and snippets.

@manudurgoni
Created March 21, 2018 09:33
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 manudurgoni/194f85fd0a8a4da946f05f66ad03a9d8 to your computer and use it in GitHub Desktop.
Save manudurgoni/194f85fd0a8a4da946f05f66ad03a9d8 to your computer and use it in GitHub Desktop.
Calculate the distance between two GPS coordinates
var rad = function(x) {
return x * Math.PI / 180;
};
var getDistance = function(p1, p2) {
var R = 6378137; // Earth’s mean radius in meter
var dLat = rad(p2.lat() - p1.lat());
var dLong = rad(p2.lng() - p1.lng());
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
Math.sin(dLong / 2) * Math.sin(dLong / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d; // returns the distance in meter
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment