Zip Code Proximity Search
December 12, 2006 6:34 AM   Subscribe

I need to add a zip code proximity search to a site. I have the registration form configured so that it stores a user's zip code with his/her profile. Now I want to give the option to "Show all users within X miles". Does anyone know if there is a web API to handle this?

If nobody knows of a web API, does anyone have a better solution?
posted by GatorDavid to Computers & Internet (10 answers total) 5 users marked this as a favorite
Hmmm..... that sounds familiar. Maybe the fine folks who run MetaFilter would share their solution with you, maybe on MetaTalk?
posted by fvox13 at 6:52 AM on December 12, 2006

If you want the do-it-yourselfer method, you can download a ZIP code database (there are others for pay), and do the math in MySQL.
posted by tmcw at 6:56 AM on December 12, 2006

All you need to do is get the lattitude and longitude of all zips in a table, then given your target zip you just select all zips +/- a bit of longitude/latitude.
posted by zeoslap at 6:59 AM on December 12, 2006

Assuming USA users and five-digit zip codes, you can use a free database which yields the lat and long of each zip code, and then the ye olde great circle calculation to do so.

Database here (there are more out there, but this seems OK).

Great circle formula:

d = EarthRadius * 2 * arcsin(min(1,sqrt((sin((lat2 - lat1)/2))^2 + cos(lat1) * cos(lat2) * (sin( lon2 - lon1)/2))^2)))

Note, there are a lot of code snippets out there, just google your preferred scripting language + great circle.
posted by maxwelton at 7:00 AM on December 12, 2006

Doh! on preview. Old and slow.

Note, I was unable to find any free databases of any country other than the US when I was involved in this many moons ago. Canada/UK post codes are very specific and many of the proprietary databases out there seem to have been built by brute force...and all were too pricey for my free web site.

Good luck!
posted by maxwelton at 7:03 AM on December 12, 2006

What language are you writing in? Rails (for example) has a ready-made plugin that does this for you.
posted by ewagoner at 7:15 AM on December 12, 2006

I wrote this calculation in PHP a while back... I broke down and bought a db of zip code to lat/long for < $20,>
Client loved it. Got to see which potential clients were within what distance of a given booked client.
posted by Wild_Eep at 7:17 AM on December 12, 2006

derail (no pun intended)...
ewagoner, what's the plugin for rails? I just hand-built some code that does this for a soon-to-be-launched website, but I'm curious to see how others might have approached the problem.
posted by lowlife at 7:26 AM on December 12, 2006

Response by poster: Thanks for the suggestions!

I found a guy that provides this as a web service. I guess I'll use this for now; I'll have to drop him a line to see if he expects this to be available permanently.
posted by GatorDavid at 7:29 AM on December 12, 2006

For google's sake, here's the Rails plugin.
posted by ewagoner at 7:38 AM on December 12, 2006

« Older Who will assess me?   |   b0rked partition table! halp! Newer »
This thread is closed to new comments.