Created
May 5, 2020 15:21
-
-
Save henryrossiter/0e8c9786706f6971eb1580600ea9a169 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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