SHARE
TWEET

Untitled

a guest Sep 17th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export function getDistanceFromLocationsInKm(
  2.   latStart: number,
  3.   longStart: number,
  4.   latEnd: number,
  5.   longEnd: number
  6. ): number {
  7.   const R = 6371; // Radius of the earth in km
  8.   const dLat = _deg2rad(latEnd - latStart);
  9.   const dLong = _deg2rad(longEnd - longStart);
  10.   const a =
  11.     Math.sin(dLat / 2) * Math.sin(dLat / 2) +
  12.     Math.cos(_deg2rad(latStart)) * Math.cos(_deg2rad(latEnd)) * Math.sin(dLong / 2) * Math.sin(dLong / 2);
  13.   const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  14.   const d = R * c; // Distance in km
  15.  
  16.   return +d.toFixed(2);
  17. }
  18.  
  19. function _deg2rad(deg) {
  20.   return deg * (Math.PI / 180);
  21. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top