Skip to content

Instantly share code, notes, and snippets.

@alanfoandrade
Created October 15, 2020 12:32
Show Gist options
  • Save alanfoandrade/84d2a79c9f2537564167ef119fb2ea60 to your computer and use it in GitHub Desktop.
Save alanfoandrade/84d2a79c9f2537564167ef119fb2ea60 to your computer and use it in GitHub Desktop.
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