Skip to content

Commit

Permalink
fix: Remove rounding. Return distance in kilometers
Browse files Browse the repository at this point in the history
  • Loading branch information
devcshort committed Nov 11, 2024
1 parent 8d26378 commit 84c5231
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/shared/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Coords = [number, number]; // [longitude, latitude]
* @see https://stackoverflow.com/questions/18883601/function-to-calculate-distance-between-two-coordinates
* @returns
*/
export function distanceBetweenCoordsInMiles(coords1: Coords, coords2: Coords) {
export function distanceBetweenCoordsInKm(coords1: Coords, coords2: Coords) {
const M = 0.621371; // Miles in a kilometer

const R = 6371; // Radius of the earth in km
Expand All @@ -28,7 +28,8 @@ export function distanceBetweenCoordsInMiles(coords1: Coords, coords2: Coords) {
Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const d = R * c; // Distance in km
return Math.round(d * M);

return d;
}

function deg2rad(deg: number) {
Expand Down

0 comments on commit 84c5231

Please sign in to comment.