Last active
June 16, 2023 23:29
-
-
Save sturmenta/4927c5748aa21fb564f54849caf101fc to your computer and use it in GitHub Desktop.
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 numToRad = (num) => (num * Math.PI) / 180; | |
export const getDistFrom2GpsLocations = ({ lat1, long1, lat2, long2 }) => { | |
lat1 = parseFloat(lat1); | |
long1 = parseFloat(long1); | |
lat2 = parseFloat(lat2); | |
long2 = parseFloat(long2); | |
const R = 6371e3; | |
const φ1 = numToRad(lat1); | |
const φ2 = numToRad(lat2); | |
const Δφ = numToRad(lat2 - lat1); | |
const Δλ = numToRad(long2 - long1); | |
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 parseInt(String(R * c), 10); //meters | |
// return R * c * 0.000621371192; // miles; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment