This is a hack that brings the power of address translation (converting a US Postal Address into a Latitude/Longitude) to the Google Maps API – something that wasn’t provided in the default distribution.
View the Demo! – Download the Code
This hack, which is completely reusable, is broken down into a couple portions.
Address Translation Proxy (written in Perl)
my $cgi = new CGI();
my $a = $cgi->param(‘a’);
my $d = get( “http://rpc.geocoder.us/service/rest?address=$a” );
$d =~ /geo:long>([^< ]*).*?geo:lat>([^< ]*)/is; print "Content-type: text/plain\n\n"; print "$1,$2";[/perl] The above code does the following:
- function GAddress( String address, Function callback );
The first argument, address, is a string representing the address that you want to translate (for example, “123 Main St. Anywhere, NY”). The second argument, callback, is a reference to a function which will be called once the translation is complete. That function will be called with two arguments:
- function callback( GPoint point, String address );
The first argument, point, will either be a GPoint representing the latitude/longitude of an address OR null, if the address does not exist. The second argument is the same address as what was sent when you called GAddress.
Now, using both of these components, it’s time to wrap them together and put them to use! If you’re interested to see what a final result looks like, check out this demo.
If you’d like to put this code to use, feel free to download the code below and give it a try!
- Copy the contents of the archive to your web directory.
- Run the following command, from the command-line (or your favorite FTP client) chmod 0755 gaddress.cgi
- Go to the Google Maps API signup page and generate an API key for the URL where you uploaded the files.
- Finally, get the API key which you generated, open index.html, and change key=CHANGEME to represent your API key.
- You should be good to go! Have fun!