Skip to content

Instantly share code, notes, and snippets.

@henryrossiter
Created May 5, 2020 15:21
Show Gist options
  • Save henryrossiter/0e8c9786706f6971eb1580600ea9a169 to your computer and use it in GitHub Desktop.
Save henryrossiter/0e8c9786706f6971eb1580600ea9a169 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