Finding distances based on Latitude and Longitude

This JavaScript uses the Haversine Formula (shown below) expressed in terms of a two-argument inverse tangent function to calculate the great circle distance between two points on the Earth. This is the method recommended for calculating short distances by Bob Chamberlain (rgc@jpl.nasa.gov) of Caltech and NASA's Jet Propulsion Laboratory as described on the U.S. Census Bureau Web site.

dlon = lon2 - lon1
dlat = lat2 - lat1
a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
c = 2 * atan2( sqrt(a), sqrt(1-a) )
d = R * c (where R is the radius of the Earth)

Note: this formula does not take into account the non-spheroidal (ellipsoidal) shape of the Earth. It will tend to overestimate trans-polar distances and underestimate trans-equatorial distances. The values used for the radius of the Earth (3961 miles & 6373 km) are optimized for locations around 39 degrees from the equator (roughly the Latitude of Washington, DC, USA).

GeoCode.com's TeleAtlas provides a form you can use to find the Latitude and Longitude for any U.S. address. I wrote a script to convert between decimal degrees and degrees/minutes/seconds formats.


First location (default: 1600 Pennsylvania Ave NW, Washington, DC)
Latitude: Longitude:
Expressed in decimal degrees

Second location (default: 1922 F St NW, Washington, DC)
Latitude: Longitude:
Expressed in decimal degrees


Results
miles
km


Disclaimer

This page is offered as a quick hack only. I threw this together as a proof-of-concept in 2002 based on a couple of articles I found on the internet. I am not an expert in geospatial anything. You should not rely on the results this page provides for anything serious, such as aircraft navigation or sailing a boat across the Atlantic (both of which people have told me they used this page to help with!). In fact, I strongly suggest you reference this page for a JavaScript implementation of the formula. But, if you insist on using this one, you are free to use the code in your own applications under the terms of the following "MIT" license (the important part of which, to me, is that you can’t sue me in case you end up crashing into a mountain because of my code):

Copyright © 2002 — 2015 Andrew Hedges

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Was this page useful to you? Loading...