Created
October 15, 2020 12:32
-
-
Save alanfoandrade/84d2a79c9f2537564167ef119fb2ea60 to your computer and use it in GitHub Desktop.
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
function deg2rad(deg) { | |
return deg * (Math.PI / 180); | |
} | |
export default function getDistanceFromLatLonInKm( | |
centerCoordinates, | |
pointCoordinates | |
) { | |
const radius = 6371; | |
const { latitude: lat1, longitude: lon1 } = centerCoordinates; | |
const { latitude: lat2, longitude: lon2 } = pointCoordinates; | |
const dLat = deg2rad(lat2 - lat1); | |
const dLon = deg2rad(lon2 - lon1); | |
const a = | |
Math.sin(dLat / 2) * Math.sin(dLat / 2) + | |
Math.cos(deg2rad(lat1)) * | |
Math.cos(deg2rad(lat2)) * | |
Math.sin(dLon / 2) * | |
Math.sin(dLon / 2); | |
const center = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
const distance = radius * center; | |
return distance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment