|
Calculate distance and bearing between
two Latitude/Longitude points
*Since the earth is not quite a
sphere, there are small errors in using spherical geometry; the earth is
actually roughly ellipsoidal (or more precisely, oblate spheroidal) with
a radius varying between about 6,378km (equatorial) and 6,357km (polar),
and local radius of curvature varying from 6,336km (equatorial meridian)
to 6,399km (polar). This means that errors from assuming spherical
geometry might be up to 0.55% crossing the equator, though generally
below 0.3%, depending on latitude and direction of travel. An accuracy
of better than 3m in 1km is good enough for me, but if you want greater
accuracy, you could refine the result by using the local radius of
curvature, as explained in the US Census Bureau
GIS FAQ.
- Notes: trig functions take arguments in radians, so
latitude, longitude, and bearings in degrees (either decimal or
degrees/minutes/seconds) need to be converted to radians, rad =
π.deg/180. When converting radians back to degrees (deg = 180.rad/π),
West is negative if using signed decimal degrees. For bearings, values
in the range -π to +π (-180° to +180°) need to be converted to 0 to +2π
(0°–360°); this can be done by (brng+2.π)%2.π where % is the modulo
operator. View page source to see JavaScript functions to handle these
conversions.
- The atan2() function widely used here takes two arguments,
atan2(y, x), and computes the arc tangent of the ratio y/x. It is more
flexible than atan(y/x), since it handles x=0, and it also returns
values in all 4 quadrants -π to +π (the atan function returns values in
the range -π/2 to +π/2).
- If you implement any formula involving atan2 in Microsoft Excel,
you will need to reverse the arguments, as
Excel
has them the opposite way around from
JavaScript
– conventional order is atan2(y, x), but Excel uses atan2(x, y)
- For miles, divide km by 1.609344
- For nautical miles, divide km by 1.852
|