Skip to content

Instantly share code, notes, and snippets.

@icetee
Last active August 11, 2018 14:07
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 icetee/e88a9bd0c254e3563df0ee06f644d701 to your computer and use it in GitHub Desktop.
Save icetee/e88a9bd0c254e3563df0ee06f644d701 to your computer and use it in GitHub Desktop.
Calc GPS Distance
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