How to Calculate the Distance Between Two Lat/Longs

Written by kelvin o'donahue
• Share
• Tweet
• Share
• Pin
• Email

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
Sort:
• Most relevant
• Most popular
• Most recent