To calculate the distance between two points specified by latitude and longitude, you can use the Haversine formula or Vincenty's formulae. Both formulas take into account the curvature of the Earth and provide reasonably accurate results. Here's a brief explanation of each method:
Haversine Formula:
The Haversine formula calculates the great-circle distance between two points on a sphere (in this case, the Earth) assuming a perfect sphere. It is simpler and faster than Vincenty's formulae but may have slightly lower accuracy, especially for longer distances.
The Haversine formula is as follows:
a = sin²(Δlat/2) + cos(lat1) * cos(lat2) * sin²(Δlon/2)
c = 2 * atan2(√a, √(1-a))
d = R * c
Where:
Δlat is the difference in latitude between the two points.
Δlon is the difference in longitude between the two points.
lat1 and lat2 are the latitudes of the two points.
R is the radius of the Earth (mean radius: 6,371 km).
You can find direct implementation at Haversine Implementations
Vincenty's Formulae:
Vincenty's formulae are more complex but provide higher accuracy for calculating the distance between two points on an ellipsoidal model of the Earth. They take into account the Earth's shape and provide accurate results for any pair of points on the globe.
There are two variations of Vincenty's formulae: the direct formula, which calculates the distance between points, and the inverse formula, which calculates the initial bearing, final bearing, and distance between points.
To use Vincenty's formulae, you'll need to implement the specific equations in your chosen programming language.
Direct Formula:
The direct formula calculates the destination point (latitude and longitude) given a starting point, initial bearing, and distance. It is useful when you know the starting point, the desired distance, and the initial bearing towards the destination.
The formula is as follows:
α1 = atan2((1 - f) * tan(lat1), cos(α0))
sinσ1 = (cos(U2) * sin(α1))^2 + (cos(U1) * sin(U2) - sin(U1) * cos(U2) * cos(α1))^2
sinσ = sqrt(sinσ1)
cosσ = sin(U1) * sin(U2) + cos(U1) * cos(U2) * cos(α1)
σ = atan2(sinσ, cosσ)
sinα = cos(U1) * cos(U2) * sin(α1) / sinσ
cos2αm = 1 - sinα^2
C = (f / 16) * cos2αm * (4 + f * (4 - 3 * cos2αm))
λ = L + (1 - C) * f * sinα * (σ + C * sinσ * (cos(2 * σ) + C * cosσ * (-1 + 2 * cos(2 * σ)^2)))
Δ = L2 - λ
L = λ
Where:
lat1, lon1: Starting point latitude and longitude in radians.
α0: Initial bearing in radians.
U1 = atan((1 - f) * tan(lat1))
U2 = atan((1 - f) * tan(lat2)), where lat2 is calculated iteratively using the formula above.
f = (a - b) / a, where a and b are the equatorial and polar radii of the Earth, respectively.
Inverse Formula:
The inverse formula calculates the distance, initial bearing, and final bearing between two points on the Earth's surface. It is useful when you know the latitude and longitude of both points.
The formula is as follows:
L = lon2 - lon1
λ = L
λʹ = 2π + atan2(y, x)
σ = atan2(yʹ, xʹ)
C = (f / 16) * cos^2αm * (4 + f * (4 - 3 * cos^2αm))
λ = L + (1 - C) * f * sinα * (σ + C * sinσ * (cos(2 * σ) + C * cosσ * (-1 + 2 * cos(2 * σ)^2)))
Where:
lat1, lon1: Latitude and longitude of the first point in radians.
lat2, lon2: Latitude and longitude of the second point in radians.
L = lon2 - lon1
U1 = atan((1 - f) * tan(lat1))
U2 = atan((1 - f) * tan(lat2))
sinα = cos(U2) * sin(L)
cosα = sqrt(1 - sinα^2)
cos^2αm = cosα^2 * cosσ^2
x = σ - sinα * sinσ
y = λʹ - sinα * sinσ
xʹ = cos(U1) * sin(U2) - sin(U1) * cos(U2) * cos(L)
yʹ = cos(U2) * sin(L)