Distance between two gps locations
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
const dist2GPS = (lat1, lng1, lat2, lng2) => { | |
lat1 = parseFloat(lat1); | |
lng1 = parseFloat(lng1); | |
lat2 = parseFloat(lat2); | |
lng2 = parseFloat(lng2); | |
const R = 6371e3; | |
const φ1 = lat1.toRad(); | |
const φ2 = lat2.toRad(); | |
const Δφ = (lat2 - lat1).toRad(); | |
const Δλ = (lng2 - lng1).toRad(); | |
const a = | |
Math.sin(Δφ / 2) * Math.sin(Δφ / 2) + | |
Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2); | |
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
// return R * c; //meters | |
return R * c * 0.000621371192; // miles; | |
}; | |
if (typeof Number.prototype.toRad === 'undefined') { | |
// eslint-disable-next-line no-extend-native | |
Number.prototype.toRad = function() { | |
return (this * Math.PI) / 180; | |
}; | |
} | |
export default dist2GPS; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment