How to Calculate the Distance Between Two Lat/Longs

Written by kelvin o'donahue
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Calculate the Distance Between Two Lat/Longs
Calculating the distance between two latitude/longitude points can be accomplished with the haversine formula. (globe image by danimages from

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.

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 = latitude 1 - latitude 2

DLONG = longitude 1 - 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,371km or 3,959 miles) to obtain the shortest distance between the two points.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.