Simple Zip Code Geocoding

mpls_20070203.jpg

The ability to geocode, or translate into latitude and longitude, postal codes is a fairly useful hack to have in your programming toolbox. Quick and dirty zip geocoding allows you to do some neat things fairly efficiently and with a minimal amount of code. Though it's U.S. centric, it allows you to add location-based functionality to your apps without requiring any real personal information to be transfered or stored.

If your application only needs to convert a zip code (or any address) into a lat/lon coordinate, say for simple mapping purposes, the easiest solution is to use the Google Maps Geocoding API. In addition to the client-side javascript functionality, you can directly query the geocoding system from php using an http request like this:

http://maps.google.com/maps/geo?q=12345&output=xml&key=yourkeyhere

Just change 12345 to the zip (or any address) that you are looking up, and "yourkeyhere" should be your Google Map API key, which you can obtain here. Developer.com has a good PHP example for making use of the returned XML in your server-side code.

Often times, it's useful to be able to do zip lookups based on a geographic region. Maybe you want a list of all zip codes within a certain radius or bounding box. Applications for this could include clustering map items that are near eachother, or searching a database for items that are nearest to a given location. For this, it's really nice to have a MySQL table that contains zip codes along with their lat/lon coordinates. Fortunately, several people have compiled this sort of information from public domain data, and you can even download a full MySQL table dump here, for free.

At this point, it's a pretty simple matter to query the database for location-based information. For instance, let's say you have a web site with a guestbook that allows guests to leave their name and zip. You could easily whip up an application that tells your guests how many other guests are in their area by using a basic bounding box with a query like this:

SELECT guest.name from guest, zipcode
WHERE guest.zip = zipcode.zip
AND zipcode.lat < [maxlat] AND zipcode.lat > [minlat]
AND zipcode.lng < [maxlng] AND zipcode.lng > [minlng]

These are just a few ideas, but hopefully this should be enough to get you started. If you have some good ideas for other geocoding applications (or any mapping/gis hacks in general), please give us a shout in the comments.

Posted by Jason Striegel | Feb 3, 2007 09:42 PM
Ajax, Google Maps, Mapping, MySQL | Permalink | Comments (3) Bookmark and Share

Recent Entries

Comments

Newest comments listed first.

Posted by: skot9000 on February 4, 2007 at 12:32 AM

Google maps app to find fast passes in San Francisco. Mass geocoded from the muni.com website using http://www.gpsvisualizer.com/

http://www.skot9000.com/muni/


Posted by: postal codes on December 27, 2007 at 10:27 PM

Search UK Postal Code (PostCode) of Place & City names in UK (Zip code, Postcode). Postal Code


Posted by: Zip Map on February 5, 2008 at 1:50 AM

I would recommend http://www.zipmap.info/ for any US zip code map.
You can use this to find any US zip code maps. You can search by (City,

State OR Zip). they have all the maps showing zip codes and major mile

streets in all of the united states. The service is free. Search for five

Digit Zip Code and find the Maps for all 50 states. A Free ZIP Code Finder

which can do wonders for you. Zip Map


Leave a comment



Bloggers

Welcome to the Hacks Blog!

Brian Jepson.Brian Jepson


Jason Striegel.Jason Striegel


Philip Torrone.Phillip Torrone



See all of the books in the Hacks Series!
Advertise here.

Recent Posts

www.flickr.com
photos in Hacks More photos in Hacks

Most read entries (last 30 days)