Location 1 | Latitude | ||
---|---|---|---|

Longitude | |||

Location 2 | Latitude | ||

Longitude |

Distance:

0 × earth radius

0 km

0 miles

(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.)

It takes advantage of the facts that:

- The shortest distance between two points on a sphere is a part of a great circle; that is, a circle with the same radius as the sphere.
- Given a chord of a circle, it's possible to find the angle of the arc.

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.

`x`= cos(`latitude`) cos(`longitude`)`y`= cos(`latitude`) sin(`longitude`)`z`= sin(`latitude`)

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.