Last active
August 11, 2018 14:07
-
-
Save icetee/e88a9bd0c254e3563df0ee06f644d701 to your computer and use it in GitHub Desktop.
Calc GPS Distance
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 toRad = function (number) { | |
return number * Math.PI / 180; | |
} | |
const calculateDistance = function (lat1, lon1, lat2, lon2) { | |
var R = 6371; // 6371 - km | 6371e3 - m | |
var dLat = toRad(lat2 - lat1); | |
var dLon = toRad(lon2 - lon1); | |
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + | |
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * | |
Math.sin(dLon / 2) * Math.sin(dLon / 2); | |
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
var d = R * c; | |
return d; | |
} | |
// Usage | |
// const MAX_DISTANCE = 2; | |
// | |
// if (distance <= MAX_DISTANCE) { | |
// console.log('Ok'); | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment