Details for computing distance using lat/long coordinates

This page explains how the distances are calculated on the page Computing distance using lat/long coordinates.

First we convert latitude and longitude to spherical coordinates. We assume the radius of the earth is 3960 miles (6373 kilometers). (To specify the earth's radius to greater precision, we have to take into account the fact that the earth is not perfectly spherical, a complication we ignore.) Therefore ρ = 3960.

The degrees east of the prime meridian gives the θ coordinate.

Finally φ is the angle from the north pole down to the geographical location. Therefore φ equals 90° minus the degrees latitude north of the equator. For example, 30° latitude corresponds to φ = 60°. For southern latitudes, φ is 90° plus the number of degrees south of the equator. For example, the Tropic of Capricorn is 23.5° south of the equator, corresponding to φ = 113.5.

We convert the θ and φ angles to radians by multiplying by 2π/360°.

Given two points in spherical coordinates (ρ, θ1, φ1) and (ρ, θ2, φ2), the arc formed by connecting the points is

ψ = arccos(sin φ1 sin φ2 cos(θ12) + cos φ1 cos φ2).

The length of the arc ψ is ρ ψ. This is the great circle distance between the two locations.

The derivation of the formula for ψ follows. Assume we are given the spherical coordinates of two locations (ρ, θ1, φ1) and (ρ, θ2, φ2). Let u and v be their respective rectangular coordinates with origin at the center of the earth.

u = ρ(cos θ1 sin φ1, sin θ1 sin φ1, cos φ1)
v = ρ(cos θ2 sin φ2, sin θ2 sin φ2, cos φ2)

If we cut the earth by the plane containing u and v, we can find the great circle containing the two locations. We find the angle ψ between the two vectors in order to find how much of the circle is taken up by the arc between the locations.

Then cos ψ is given by the dot product of the normalized vectors u/|u| and v/|v|. Note that normalizing u and v simply cancels out the factor of ρ. It follows that cos ψ equals

sin φ1 sin φ2 (cos θ1 cos θ2 + sin θ1 sin θ2) + cos φ1 cos φ2

or

sin φ1 sin φ2 cos(θ1 - θ2) + cos φ1 cos φ2

and so the expression above for ψ follows.