Skip to content

Commit

Permalink
refactor(distanceBetweenPoints): optimize (googlemaps#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored May 24, 2023
1 parent df9742a commit e0f9a11
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/algorithms/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ export const distanceBetweenPoints = (
const R = 6371; // Radius of the Earth in km
const dLat = ((p2.lat - p1.lat) * Math.PI) / 180;
const dLon = ((p2.lng - p1.lng) * Math.PI) / 180;
const sinDLat = Math.sin(dLat / 2);
const sinDLon = Math.sin(dLon / 2);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
sinDLat * sinDLat +
Math.cos((p1.lat * Math.PI) / 180) *
Math.cos((p2.lat * Math.PI) / 180) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
sinDLon *
sinDLon;
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
};
Expand Down

0 comments on commit e0f9a11

Please sign in to comment.