compute.js 594 B

123456789101112131415161718192021
  1. function computeDistance(lat1, lng1, lat2, lng2) {
  2. var radLat1 = (lat1 * Math.PI) / 180.0;
  3. var radLat2 = (lat2 * Math.PI) / 180.0;
  4. var a = radLat1 - radLat2;
  5. var b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
  6. var s =
  7. 2 *
  8. Math.asin(
  9. Math.sqrt(
  10. Math.pow(Math.sin(a / 2), 2) +
  11. Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
  12. )
  13. );
  14. s = s * 6378.137; // EARTH_RADIUS;
  15. s = Math.round(s * 10000) / 10000;
  16. return s;
  17. }
  18. module.exports = {
  19. computeDistance
  20. };