pf » Calculating Distance in Miles from Latitude and Longitude
Calculating Distance in Miles from Latitude and Longitude
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.
add to del.icio.us
| Tags: maps, latitude, longitude, geo, gps, geocoding, sql, mysql, google
Related Entries
- Cheat Sheet Roundup - Over 30 Cheatsheets for developers - September 1, 2005
- INFORMATION_SCHEMA Support in MySQL, PostgreSQL - February 18, 2008
- Backwards LIKE Statements - January 10, 2007
- Sphinx - Open Source SQL Full Text Search Engine - November 1, 2006
- Job Maps - Indeed Job Search + Google Maps - September 11, 2006
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.
(Greets to bazmod!)
dy = radius * (lat1 - lat0); dx = radius * cos(lat1) * (lon1 - lon0); distance = sqrt(dy*dy + dx*dx)
radius = earth radius lat,lon in radians
- CFSCRIPT Cheatsheet
- 3 New Image Effects for ColdFusion 8
- Googlebot to Submit Web Forms
- ColdFusion 8 Update 1 Fixes some Image Processing Quirks
- 10 Most Useful Image Functions in ColdFusion 8
- Speaking at NYC CFUG This Week
- Adobe AIR Tutorial for HTML / JavaScript Developers
- INFORMATION_SCHEMA Support in MySQL, PostgreSQL
RSS
Pete Freitag is a software engineer, and web developer located in










