Skip to content

Instantly share code, notes, and snippets.

@michaeljymsgutierrez
Created October 9, 2019 09:16
Show Gist options
  • Select an option

  • Save michaeljymsgutierrez/fd6a1b5d29e7f0c43e7faf09a83bead8 to your computer and use it in GitHub Desktop.

Select an option

Save michaeljymsgutierrez/fd6a1b5d29e7f0c43e7faf09a83bead8 to your computer and use it in GitHub Desktop.
/**
* Set of functions for calculating distance
*/
const getDistance = (lat1, lon1, lat2, lon2) => {
let R = 6371;
let dLat = deg2rad(lat2 - lat1);
let dLon = deg2rad(lon2 - lon1);
let a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) *
Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
let d = R * c;
return d;
};
const deg2rad = deg => {
return deg * (Math.PI / 180);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment