Google Address Translation


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)
This portion of this project queries the open API provided by Geocoder, which offers free address translation for any postal address in the United States. (If you live in Canada, you may want to check out Geocoder.ca). The code is very very simple, the only reason why it’s needed is due to the fact that Javascript applications can’t make queries to services that aren’t on the same domain. The code is so short, I can show it here:

  1. #!/usr/bin/perl
  2.  
  3. use CGI;
  4. use LWP::Simple;
  5.  
  6. my $cgi = new CGI();
  7. my $a = $cgi->param('a');
  8. my $d = get( "http://rpc.geocoder.us/service/rest?address=$a" );
  9.  
  10. $d =~ /geo:long>([^< ]*).*?geo:lat>([^< ]*)/is;
  11. print "Content-type: text/plain\n\n";
  12. print "$1,$2";

The above code does the following:

  • It gets the address from the browser – a query which looks something like this: gaddress.cgi?a=123+Main+St+Anywhere,+NY.
  • A query is made to the Geocoder service provided at this URL: http://rpc.geocoder.us/service/rest
  • Finally, the latitude and longitude are parsed out of the results and returned to the Javascript client.
  • Javascript Addressing Querying
    This simple function, written in Javascript, makes a query to the Address Translation Proxy asking it to convert an address into a Google GPoint. This function has two parameters that need to be taken into consideration:

    1. 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:

    1. 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!

    Download

    • gaddress.tar.gz – Contains sample index.html, Javascript Query Function (gaddress.js), and Address Translation Proxy (gaddress.cgi). To install:
      1. Copy the contents of the archive to your web directory.
      2. Run the following command, from the command-line (or your favorite FTP client) chmod 0755 gaddress.cgi
      3. Go to the Google Maps API signup page and generate an API key for the URL where you uploaded the files.
      4. Finally, get the API key which you generated, open index.html, and change key=CHANGEME to represent your API key.
      5. You should be good to go! Have fun!

    Posted: July 10th, 2005


    If you particularly enjoy my work, I appreciate donations given with Gittip.

    78 Comments (Show Comments)



    Comments are closed.
    Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


    Secrets of the JavaScript Ninja

    Secrets of the JS Ninja

    Secret techniques of top JavaScript programmers. Published by Manning.

    Ukiyo-e Database and Search

    Ukiyo-e.org

    Japanese woodblock print database and search engine.


    John Resig Twitter Updates

    @jeresig

    Infrequent, short, updates and links.


    via Ad Packs