Pete Freitag Pete Freitag

Calculating Distance in Miles from Latitude and Longitude

Updated on December 07, 2023
By Pete Freitag

The amount of data out there via API's is increadible these days. For instance you can take an address, and get the latitude and longitude using Google's GeoCoding API.

I am using this API along with some others to build a pretty some interesting stuff (more on that when its public).

Today I needed to calculate the distance between two points, I found a bunch of formulas here (link removed, no longer working) to convert two lats and longs into miles. They had some more complicated formulas, but I went with an easier one because approximate accuracy was sufficient. Here's how the formula translated into SQL (tested on MySQL):

SELECT id, place_name,
ROUND( SQRT( POW((69.1 * (#Val(arguments.latitude)# - latitude)), 2) + POW((53 * (#Val(arguments.longitude)# - longitude)), 2)), 1) AS distance
FROM places
ORDER BY distance ASC

I am also rounding it to one decimal place, which you can remove if you want.

Speaking of mileage, if you are looking for a Web Application to track business mileage checkout

maps latitude longitude geo gps geocoding sql mysql google

Calculating Distance in Miles from Latitude and Longitude was first published on January 18, 2007.

If you like reading about maps, latitude, longitude, geo, gps, geocoding, sql, mysql, or google then you might also like:

Discuss / Follow me on Twitter ↯


does that not make the assumption that each longitude line is always 69.1 miles apart?

if you remember that the lines are drawn on a globe, you'll realise 69.1 is only correct at two exact latitudes, north and south of the equator.
by Kae Verens on 01/20/2007 at 2:59:28 PM UTC
A more accurate yet still reasonably simple implementation would be the haversine method. This takes into account the average curvature of the Earth's surface. I recently combined this into a CF app alone with converting GRE/GRN to Lat/Long so I could do a distance calculation between a GRE/GRN co-ord and a Lat/Long co-ord.

(Greets to bazmod!)
by David Stockton on 01/24/2007 at 8:09:25 AM UTC
Hey Pete,
I built with ColdFusion and MySql and have tons of Geo-related stuff if you need anything. The JavaScript does a lot of the Google Maps API stuff but there is some CF and MySql code I can share.
by Adam Howitt on 01/26/2007 at 6:17:05 AM UTC
Of course you don't want to run a calc like this for all data points in your database. You can setup a box around your point using pretty simple math and apply the floating point calcs to all points within the box. 1 degree of arc at the earth's surface = 60nautical miles. I've been meaning to write an article about this but just haven't had the time.
by Tom Nunamaker on 08/11/2007 at 8:01:17 AM UTC
I did just as Tom suggested recently with a mySQL stored proceedure to expand a box out from the point, several iterations, until I have the desired number of locations in a temp table or until i've reached my maximum box size for the data to still be meaningful, then I calced the distance using the law of haversines on only the subset of data points. It runs extremely efficiently as compared to just calcing the law of haversines in a WHERE clause against all data points.
by Bill Blaettler on 01/28/2008 at 8:03:54 AM UTC