Skip to content

Instantly share code, notes, and snippets.

@mrummuka
Forked from henryrossiter/location.js
Created April 4, 2022 17:21
Show Gist options
  • Save mrummuka/a9a127ba38dfee9b0bfefbfbe63b5447 to your computer and use it in GitHub Desktop.
Save mrummuka/a9a127ba38dfee9b0bfefbfbe63b5447 to your computer and use it in GitHub Desktop.
function cosineDistanceBetweenPoints(lat1, lon1, lat2, lon2) {
const R = 6371e3;
const p1 = lat1 * Math.PI/180;
const p2 = lat2 * Math.PI/180;
const deltaP = p2 - p1;
const deltaLon = lon2 - lon1;
const deltaLambda = (deltaLon * Math.PI) / 180;
const a = Math.sin(deltaP/2) * Math.sin(deltaP/2) +
Math.cos(p1) * Math.cos(p2) *
Math.sin(deltaLambda/2) * Math.sin(deltaLambda/2);
const d = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)) * R;
return d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment