Skip to content

Instantly share code, notes, and snippets.

@XerxesNoble
Last active February 22, 2018 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XerxesNoble/8df7bc81103bff2acd95c82996f02889 to your computer and use it in GitHub Desktop.
Save XerxesNoble/8df7bc81103bff2acd95c82996f02889 to your computer and use it in GitHub Desktop.
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