Latitude/longitude distance

Location 1Latitude
Longitude
Location 2Latitude
Longitude

Distance:
0 × earth radius
0 km
0 miles

Options

(Note: This assumes the Earth is a perfect sphere, and therefore has at most 2 digits of precision. Also this is the distance if you travelled in a straight line (or as close as possible) on the Earth's surface; if you have to worry about where roads are it'll obviously be a longer travel distance.)

How it works

It takes advantage of the facts that:

First I convert spherical coordinates (latitude and longitude) into Cartesian coordinates (x, y, z). To make things easier, I assume the radius is 1. Latitude and longitude are expressed in degrees usually, and sin and cos tend to take radians, so the angles have to first be converted to radians.

Then I calculate the straight-line distance (which goes through the earth, rather than on its surface), using the usual formula for that (sqrt(x² + y² + z²)). This line segment is also the chord of a great circle.

I then calculate the angle of the arc, using the inverse of the formula to calculate the length of a chord given an angle. (I used cos-1(1 - chordlength²/2); Wikipedia gives a different formula, which I think is equivalent.) The angle in radians, by definition, is the same as the arclength of that portion of the great circle, and therefore the distance along the sphere between the two points. Then all that's left is to multiply it by the radius of the Earth, which I got from Wikipedia.