Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. function calcDistanceBetweenToPoints (point1, point2) {
  2. const EARTH_RADIUS = 6371e3; // metres
  3.  
  4. const latitude1 = point1.lat * Math.PI / 180;
  5. const latitude2 = point2.lat * Math.PI / 180;
  6.  
  7. const deltaLatitude = (point2.lat - point1.lat) * Math.PI / 180;
  8. const deltaLongitude = (point2.lon - point1.lon) * Math.PI / 180;
  9.  
  10. const angle = Math.sin(deltaLatitude/2) * Math.sin(deltaLatitude/2) +
  11. Math.cos(latitude1) * Math.cos(latitude2) * Math.sin(deltaLongitude/2) * Math.sin(deltaLongitude/2);
  12.  
  13. const theta = 2 * Math.atan2(Math.sqrt(angle), Math.sqrt(1 - angle));
  14.  
  15. const distance = EARTH_RADIUS * theta;
  16.  
  17. return distance; //in metres
  18. }
  19.  
  20. let result = calcDistanceBetweenToPoints({
  21. lat: -33.2569755, lon: 151.5492134
  22. },{
  23. lat: -33.733914, lon: 150.945014
  24. })
  25.  
  26. console.log(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement