Skip to content

Instantly share code, notes, and snippets.

@andion
Created July 30, 2020 09:51
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 andion/29369977a7b0077d38848d66686fb288 to your computer and use it in GitHub Desktop.
Save andion/29369977a7b0077d38848d66686fb288 to your computer and use it in GitHub Desktop.
const EARTH_RADIUS_IN_METERS = 6371000;
const toRad = value => value * Math.PI / 180;
const calculateDistance = (startWaypoint, endWaypoint) => {
const dLat = toRad(endWaypoint.lat - startWaypoint.lat);
const dLon = toRad(endWaypoint.lon - startWaypoint.lon);
const startLat = toRad(startWaypoint.lat);
const endLat = toRad(endWaypoint.lat);
const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(startLat) * Math.cos(endLat);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return EARTH_RADIUS_IN_METERS * c;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment