Created
July 30, 2020 09:51
-
-
Save andion/29369977a7b0077d38848d66686fb288 to your computer and use it in GitHub Desktop.
This file contains 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 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