Last active
February 22, 2018 04:21
-
-
Save XerxesNoble/8df7bc81103bff2acd95c82996f02889 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 measure(lat1, lon1, lat2, lon2){ // generally used geo measurement function | |
const { PI, cos, sin, sqrt, atan2 } = Math // math fns | |
const R = 6378.137 // Radius of earth in KM | |
// PI is equivilant to 180 degrees, so divide PI by 180 | |
// and you get how many radians constitute a single degree | |
const radsInSingleDegree = (PI / 180) | |
// Get the amount of radians between the two coordinates | |
const [dLat, dLon] = [ | |
(lat2 * radsInSingleDegree) - (lat1 * radsInSingleDegree), | |
(lon2 * radsInSingleDegree) - (lon1 * radsInSingleDegree), | |
] | |
const a = sin(dLat / 2) * sin(dLat / 2) + cos(lat1 * radsInSingleDegree) * cos(lat2 * radsInSingleDegree) * sin(dLon / 2) * sin(dLon / 2) | |
var c = 2 * atan2(sqrt(a), sqrt(1-a)) | |
const km = (R * c) // kilometers | |
return km * 1000; // meters | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment