Created
February 21, 2024 08:30
-
-
Save av01d/17df9b909df1cddcd3ff49c03ca794ea to your computer and use it in GitHub Desktop.
Javascript Point2PointDistance: Caculate distance (in m) between 2 lat/lng points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
console.log(Point2PointDistance(52.25,5.77,52.35,5.95))