Leland USA

Tuesday, February 14, 2006

I was working on a project where we want to integrate google maps into a form page to induce people to enter their address information. We basically want the map to update dynamically as the person is entering their information. Integrating google maps is rather simple. Just sign up for an account with google and follow the instructions. However google generate maps based on longitude and latitude coordinates, so it's necessary for us to be able to get those coordinates as the user is typing.

There are plenty of free services out there that will do this for you, but after trying a few , we found all of them to be pretty slow. We realized we need the database of addresses and their coordinates on our local server. Here's how we did it. Many thanks to Dan Egnor (winner of the 2002 Google programming contest)

You can get an address database from US Department of Census and build an index yourself. But since we're lazy, we just downloaded a prebuilt index from Dan Egnor's website http://dan.egnor.name/google.html Unarchive it and save it in your cgi-bin folder.From Dan Egnor's website, you can also download the files need to build the executables that will query the index. You can go through the files and build everything according to the README to see how everything works. You can basically use the files as is if you know what you're doing.

We, however, want to modify Dan Egnor's program so we don't have to do too much parsing later on. Open up the geo-client.c file in your favorite editor and replace the main function with the following

Once you save, do make clean. Then make. Test everything out by typeing in something like ./geo-client "4910 Michoud Blvd. New Orleans, LA 70129" on the command line. You should see the longtitude and latitude coordinates.

If everything works, copy all the executables to your cgi-bin folder. Now you need to create a cgi file to interface between the geo-client and your website. We modified Dan Egnor's query.cgi file to the following

#!/usr/bin/python# Copyright 2002 Daniel Egnor. See LICENSE file.## This is a simple CGI script which invokes 'idx-client' to provide a# Web interface to the geo-search functionality. It outputs # references to MapBlast to draw maps of query results.## Many many thanks to Tessa Lau for help with Python!

Test out query.cgi with your browser by passing it a query string ?a=Your_Address_Here.

If everything works, you're ready to integrate this with javascript and have some ajax action on your website. You can probably go from here, but if not, check out this excellent totorial http://ejohn.org/projects/gaddress/

You can download scripts from John Resig. Just don't use his cgi script. Used the one you just made.