All files / src/utils geo.ts

0% Statements 0/14
100% Branches 0/0
0% Functions 0/2
0% Lines 0/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                                                           
export function isNearCoordinates(
  coord1: ICoordinates,
  coord2: ICoordinates,
  radiusInKm: number
): boolean {
  // Earth's radius in kilometers
  const earthRadius = 6371;

  // Convert latitude and longitude to radians
  const lat1 = toRadians(coord1.lat);
  const lon1 = toRadians(coord1.lng);
  const lat2 = toRadians(coord2.lat);
  const lon2 = toRadians(coord2.lng);

  // Haversine formula
  const dLat = lat2 - lat1;
  const dLon = lon2 - lon1;
  const a =
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  const distance = earthRadius * c;
 
  return distance <= radiusInKm;
}
 
function toRadians(degrees: number): number {
  return degrees * (Math.PI / 180);
}