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.
8 thoughts on “Simple Zip Code Geocoding”
Comments are closed.
ADVERTISEMENT
Join Make: Community Today
the newest version of putty now supports serial connections:
http://www.chiark.greenend.org.uk/~sgtatham/putty/changes.html
just found this article but it seems they no longer offer this free version, or they are hiding it extremely well on their site. I’m using PuTTY now instead as they recently added serial connection cabability. It’s free here: http://www.chiark.greenend.org.uk/~sgtatham/putty/
Hilgreave in NOT FREE!
n5pgo, I’m sorry to hear that they removed the free version. But as somerandomguy said, PuTTY is free, and it now supports serial connections. I’ve been using it for a while now myself, and I don’t use HyperTerminal any more.
Free HyperTerminal Personal Edition is not gone from Hilgraeve. They simply “orphaned” the download page so as to confuse people and wrongfully force them to buy their Business Edition. Here it is:
http://www.hilgraeve.com/htpe/download.html