Skip to content

Instantly share code, notes, and snippets.

@steniowagner
Created December 9, 2017 17:32
Show Gist options
  • Save steniowagner/c7b2d0aa3dd8b13fbef52dacc9360f46 to your computer and use it in GitHub Desktop.
Save steniowagner/c7b2d0aa3dd8b13fbef52dacc9360f46 to your computer and use it in GitHub Desktop.
calculateDistanceBetweenCoordinates = (firstCoordinate, secondCoordinate) => {
const PI = 0.017453292519943295;
var haversine = 0.5 - Math.cos((secondCoordinate.latitude - firstCoordinate.latitude) * PI) / 2 +
Math.cos(firstCoordinate.latitude * PI) * Math.cos(secondCoordinate.latitude * PI) *
(1 - Math.cos((secondCoordinate.longitude - firstCoordinate.longitude) * PI)) / 2;
return 12742 * Math.asin(Math.sqrt(haversine)); // 2 * EARTH_RADIUS (6371);
}
console.log(calculateDistanceBetweenCoordinates(first, second) * 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment