Database - Search by Address

Generate your own DISTANCE-CALCULATOR-FUNCTION, which will calculate the distance between two Points on a map (using longitude-koordinates, and latitude-koordinates)

Someting like…

function calculateDistance(lat1, lon1, lat2, lon2) {
  const earthRadius = 6371; // in kilometers

  const toRadians = (degrees) => {
    return degrees * (Math.PI / 180);
  };

  const deltaLat = toRadians(lat2 - lat1);
  const deltaLon = toRadians(lon2 - lon1);

  const a =
    Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
    Math.cos(toRadians(lat1)) *
      Math.cos(toRadians(lat2)) *
      Math.sin(deltaLon / 2) *
      Math.sin(deltaLon / 2);

  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

  const distance = earthRadius * c;
  return distance;
}

// Example usage:
const latitude1 = 40.7128; // latitude of position 1
const longitude1 = -74.0060; // longitude of position 1

const latitude2 = 34.0522; // latitude of position 2
const longitude2 = -118.2437; // longitude of position 2

const distance = calculateDistance(
  latitude1,
  longitude1,
  latitude2,
  longitude2
);

console.log(`The distance between the two positions is ${distance} kilometers.`);