Skip to content

Instantly share code, notes, and snippets.

@steniowagner
Created January 27, 2019 01:46
Show Gist options
  • Save steniowagner/2e76570adbe9cfbf93e90d8eb55ec037 to your computer and use it in GitHub Desktop.
Save steniowagner/2e76570adbe9cfbf93e90d8eb55ec037 to your computer and use it in GitHub Desktop.
Calculate the distance between two coordinates (lat-lng).
const parseDegreeToRadians = (degrees) => degrees * (Math.PI / 180);
const calculateDistanceBetweenTwoCoordinates = (firstCoordinate, secondCoordinate) => {
const EARTH_RADIUS = 6371;
const deltaLatitude = parseDegreeToRadians(secondCoordinate.latitude - firstCoordinate.latitude);
const deltaLongitude = parseDegreeToRadians(secondCoordinate.longitude - firstCoordinate.longitude);
const haversinesInnerTerm = Math.sin(deltaLatitude / 2) * Math.sin(deltaLatitude / 2) +
Math.cos(parseDegreeToRadians(firstCoordinate.latitude)) * Math.cos(parseDegreeToRadians(secondCoordinate.longitude)) *
Math.sin(deltaLongitude / 2) * Math.sin(deltaLongitude / 2);
const haversinesOutterTerm = 2 * Math.atan2(Math.sqrt(haversinesInnerTerm), Math.sqrt(1 - haversinesInnerTerm));
const distance = EARTH_RADIUS * haversinesOutterTerm;
return distance;
};
module.exports = calculateDistanceBetweenTwoCoordinates;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment