Skip to content

Instantly share code, notes, and snippets.

@av01d
Created February 21, 2024 08:30
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 av01d/17df9b909df1cddcd3ff49c03ca794ea to your computer and use it in GitHub Desktop.
Save av01d/17df9b909df1cddcd3ff49c03ca794ea to your computer and use it in GitHub Desktop.
Javascript Point2PointDistance: Caculate distance (in m) between 2 lat/lng points
const Point2PointDistance = (lat1, lon1, lat2, lon2) => {
const toRad = num => num * Math.PI / 180; // Converts numeric degrees to radians
const R = 6371000, // earth radius, in m
dLat = toRad(lat2 - lat1),
dLon = toRad(lon2 - lon1);
lat1 = toRad(lat1);
lat2 = toRad(lat2);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
//console.log(Point2PointDistance(52.25,5.77,52.35,5.95))
@av01d
Copy link
Author

av01d commented Feb 21, 2024

Example: console.log(Point2PointDistance(52.25,5.77,52.35,5.95))

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