Skip to content

Instantly share code, notes, and snippets.

@manix
Last active October 3, 2023 18:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save manix/7ce097c73728e07178af74cb4c62a341 to your computer and use it in GitHub Desktop.
Save manix/7ce097c73728e07178af74cb4c62a341 to your computer and use it in GitHub Desktop.
Distance between points simplified
getDistanceFromLatLonInM(lat1, lon1, lat2, lon2) {
var deg2rad = deg => deg * 0.017453293;
var a =
Math.pow(Math.sin(deg2rad(lat2 - lat1) / 2), 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.pow(Math.sin(deg2rad(lon2 - lon1) / 2), 2);
return 12742000 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}
@bdaneshgar
Copy link

is this in meters or miles?

@manix
Copy link
Author

manix commented Oct 25, 2019

Meters, as it says in the name, conforming to Si.

@bdaneshgar
Copy link

meters is usually lowercased, also line 2 should be 0.017453293, not 0,017453293

@manix
Copy link
Author

manix commented Jan 11, 2020

meters is usually lowercased, also line 2 should be 0.017453293, not 0,017453293

In the function name it goes as a separate word "Meters" that's why it is a capital letter. Also I have no idea how a comma ended up in the number, I fixed it thanks for the pointer.

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