Calculating Distance in Miles from Latitude and Longitude

January 18, 2007

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 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 sufficent. 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

Related Entries

16 people found this page useful, what do you think?


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.
dude... "increadible"? Come on, man, at least do a spell check.
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!)
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.
looks like you're missing the cos term: dy = radius * (lat1 - lat0); dx = radius * cos(lat1) * (lon1 - lon0); distance = sqrt(dy*dy + dx*dx) radius = earth radius lat,lon in radians
You can receive up-to-date zipcodes, along with latitude and longitude, county, and country information from You can also download complete geographical databases for cities, land features, water features, and man made features around the world. You can then use this data within your calculations to make sure you have the most up-to-date data.
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.
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.
My version:

# Your lat/long of preferences
SET @arguments_latitude = 51.355887;
SET @arguments_longitude = -2.949691;

# Make a distance query
POW((69.1 * (@arguments_latitude - p.latitude)), 2) +
POW((53 * (@arguments_longitude - p.longitude)), 2)
, 1) AS distance
FROM postcodes p
ORDER BY distance DESC;
<dskfjb sdlf>

Post a Comment


Spell Checker by Foundeo

Recent Entries


did you hack my cf?