Hobbies And Interests
Home  >> Science & Nature >> Nature

How to Calculate the Distance Between Two Lat/Longs

Determining the distance between latitude-longitude points is not as straightforward as calculating the distance between XY points because the distance between lines of longitude varies with latitude. The haversine formula for great-circle distances is generally used for this calculation.
  1. Calculate Latitutde-Longitude Difference

    • Convert all latitude and longitude values to radians. Calculate the difference in latitude (DLAT) and longitude (DLONG) between the two points:

      DLAT = latitude1 - latitude2

      DLONG = longitude1 - longitude 2

    Interim Calculation Step 1

    • Calculate the value of an intermediate value A with the following formula:

      A = sin(DLAT / 2)^2 + cos(latitude1) * cos(latitude2) * sin(DLONG / 2)^2

      "Sin" and "cos" are the trigonometric functions sine and cosine and the caret (^) indicates raising to a power.

    Interim Calculation Step 2

    • Calculate the value of an intermediate variable C by the following formula:

      C = 2 * (atan2(A^0.5, (1-A)^0.5))

      Atan2 is the trigonometric function arctangent.

    Calculate Distance

    • Multiply the intermediate value C by the radius of the Earth (6,371 km or 3,959 miles) to obtain the shortest distance between the two points.


https://www.htfbw.com © Hobbies And Interests