calculate distance between two coordinates formula javascript

To calculate the distance between two coordinates in Node.js using JavaScript, you can use the Haversine formula. The Haversine formula is a commonly used formula to calculate the distance between two points on a sphere based on their latitudes and longitudes.

Here's the step-by-step explanation of the formula:

  1. Define the two coordinates:
  2. Let's say the first coordinate is (lat1, lon1) and the second coordinate is (lat2, lon2).
  3. lat1, lon1 represent the latitude and longitude of the first point, respectively.
  4. lat2, lon2 represent the latitude and longitude of the second point, respectively.

  5. Convert the latitude and longitude from degrees to radians:

  6. Since the Haversine formula uses radians, you need to convert the latitude and longitude from degrees to radians.
  7. You can use the following formula to convert degrees to radians: radians = degrees * (π/180).

  8. Calculate the differences in latitudes and longitudes:

  9. Calculate the differences between latitudes and longitudes of the two points.
  10. You can calculate the differences using the following formulas:

    • Δlat = lat2 - lat1
    • Δlon = lon2 - lon1
  11. Apply the Haversine formula:

  12. The Haversine formula calculates the distance between two points on a sphere using the differences in latitudes and longitudes.
  13. You can use the following formula to calculate the distance:

    • a = sin²(Δlat/2) + cos(lat1) cos(lat2) sin²(Δlon/2)
    • c = 2 * atan2(√a, √(1-a))
    • distance = R * c
    • R represents the radius of the Earth (mean radius = 6,371 km)
    • atan2 is a mathematical function that computes the principal value of the arc tangent of y/x, using the signs of both arguments to determine the quadrant of the return value.
  14. Return the distance:

  15. The calculated distance represents the distance between the two coordinates in kilometers.

Here's an example of how you can implement this in JavaScript:

function calculateDistance(lat1, lon1, lat2, lon2) {
  const R = 6371; // Radius of the Earth in km

  const dLat = (lat2 - lat1) * (Math.PI / 180); // Difference in latitude
  const dLon = (lon2 - lon1) * (Math.PI / 180); // Difference in longitude

  const a =
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(lat1  (Math.PI / 180)) 
      Math.cos(lat2  (Math.PI / 180)) 
      Math.sin(dLon / 2) *
      Math.sin(dLon / 2);

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

  const distance = R * c; // Distance in km

  return distance;
}

const lat1 = 52.5200; // Latitude of the first point
const lon1 = 13.4050; // Longitude of the first point
const lat2 = 51.5074; // Latitude of the second point
const lon2 = -0.1278; // Longitude of the second point

const distance = calculateDistance(lat1, lon1, lat2, lon2);
console.log(distance); // Output the distance in kilometers

This code snippet calculates the distance between two coordinates using the Haversine formula. You can provide the latitude and longitude of the two points and it will output the distance in kilometers.