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:
The above code does the following:
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:
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:
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:
- 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!


Jim Spice (July 11, 2005 at 1:38 am)
VERY nice. Saved me a lot of work. I'm a perl dunce.
Spice
nate (July 11, 2005 at 10:41 am)
Perfect! I was using Google's own Javascript/XML output to get that because I'm not so familiar with Perl. Thanks for the script. Outstanding!
Geoff (July 11, 2005 at 12:40 pm)
Nicely done - thanks for the post :)
weinig (July 11, 2005 at 2:58 pm)
This is really nicely done. Especially since geocoder.us seems to provide the correct long/lat for my address, where as Google's is quite mistaken. And I live in NYC, which you would think would be easy. Thanks .
Google Blogoscoped (July 11, 2005 at 3:42 pm)
Google Maps API Address Look-up
John Resig shows how to translate any US address into a latitude/ longitude value pair to be used with Google Maps. (I hope Google makes address look-up an internal Maps API feature.)
Pose Show (July 13, 2005 at 8:45 am)
输入地名,找到其经度和纬度
Google Maps衍生出了很多好玩的东东。
这个站点(Google Address Translation)可以找到你输入的地名对应的经度和纬度值,不过只限于美国地区。
作者也给出了其实现方法(用perl写的)。
Wayne Sturman (July 17, 2005 at 7:11 pm)
I can't seem to get it to work-
What specific directories do I need to put:
index.html
gaddress.cgi
gaddress.js
also- the form element doesn't seem to have a method or an action- please advise if you can
Thanks
Wayne
John Resig (July 17, 2005 at 9:15 pm)
Wayne There is no method or action on the form element because all of the 'dirty work' is handled by Javascript and is never sent to a form. All the files that you mentioned go in the same directory. gaddress.cgi needs to have its permissions set to '0755'. If you're having difficulty with this, please let me know.
Wayne Sturman (July 18, 2005 at 9:42 pm)
I put all three files in my home directory and even tried putting gaddress.cgi into my cgi-bin directory- ai get the error message " Address not found..."
I get the fram and blank map- grey backround from the google map API-
I can't seem to figure out what I am doing wrong- any help would be appreciated
Wayne
John Resig (July 19, 2005 at 8:11 am)
Wayne My guess is that the gaddress.cgi script is having trouble executing. You need to make sure that
You uploaded the script in ASCII mode (otherwise it'll be corrupted, and not execute properly).
The script has execute permissions (for example, 'chmod 0755 gaddress.cgi').
You need to make sure that you have the LWP module installed. From the command-line type 'perl -e 'use LWP::Simple;'. If you see an error, then the LWP module needs to be installed.
If you can confirm that all of these have, in fact, been completed - and you are still receiving errors - then please let me know.
DK (July 21, 2005 at 4:48 pm)
This is not working for me. Please try my site out and let me know what you think the problem might be. http://www.ezbidder.net
Here is what I did... I downloaded all 3 files.
I replaced the key with my known working key.
I uploaded the 3 files to http://www.ezbidder.net
I changed the 0755 permission.
When I load the page now i get invalid arguement error. I have not changed anything in the script samples other then the key.
Any ideas?
John Resig (July 22, 2005 at 7:54 pm)
DK: I just went to your page and it worked - are you still having problems?
Brian (July 24, 2005 at 12:41 pm)
Wow, awesome work. I was just asking my friend yesterday if something like this existed.
I tried to put it on my own site, but I too am getting the gray screen. Any suggestions for me?
http://www.brianstucki.com/gaddress
Eugene Kelly (July 29, 2005 at 1:45 pm)
Please help.
I am recieving a grey screen. I have followed all of the steps:
Have a working google api key.
Have LWP installed.
CHMOD 0755 the .cgi file.
Path information is fine.
Are there any other problems that you can think of that might be causing the grey screen?
Thanks,
Eugene Kelly
Eugene Kelly (July 29, 2005 at 2:49 pm)
Figured it out.
Apache by default will only run .cgi scripts in the cgi-bin.
Eugene Kelly (August 1, 2005 at 8:18 am)
Im working on it now but would anyone be able to post a modified version of John's code that runs through a php loop aquiring lat/long for multiple addresses.
Eugene Kelly (August 1, 2005 at 8:42 am)
Here is the code to return the lat/long in a php variable. From here you can put this block of code into a loop and run multiple requests. The only file needed from John's code is the gaddress.cgi file.
";
//Do have to urlencode string as you can see the '%20' is necessary
$html = implode(',', file('http://yoursite.com/cgi-bin/gaddress.cgi?a=505%20University%20Ave%20Rochester,NY'));
//Code stored in array
$html = explode(",", $html);
?>
Dana (August 1, 2005 at 11:37 am)
Hi,
I have everything installed and running fine. My question has to do
with creating the URL string which will return a map for a given
location, e.g., I tried:
http://weekendneworleans.com/maps.php?a=100%20magazine%20st,%20new%20orleans,%20la
or
http://weekendneworleans.com/maps.php?a=100+magazine+st,new+orleans,La
but neither URLs are working
http://weekendneworleans.com/gaddress.pl?a=100+magazine+st,new+orleans,La
is returning Lat/Long perfectly
I know this is something rediculous, but I'm not seeing it
after 12 hours straight. Any help is much appreciated
Matt J (August 28, 2005 at 9:24 am)
Hey John -- great idea. Having trouble testing this on my home machine. Windows XP. I've got the 2.0 windows distribution of apache installed and working, Active Perl 5.8.7 installed and working. I've copied the cgi file into both the htdocs and the cgi-bin directory (just in case). I've got the Google API key and I know that's working. The only instruction you give that I can't do is the CHMOD -- not on windows anyway. Could that be the problem?
I get the Google Map with the grey screen and an error message from IE that says:
"
Line 73
Char 87
Error: invalid argument
Code: 0
"
Of course, it's not an invalid argument in the files you included (there ain't 73 lines in any of 'em).
John Resig (August 28, 2005 at 10:24 am)
Matt Try running the script directly, something like this:
http://myserver/gaddress.cgi?a=505+University+Ave+Rochester,+NY
If that script generates an error - then there's definitely something up. Let me know if that's the case, or not.
Matt J (August 28, 2005 at 3:58 pm)
John,
Thanks -- I overlooked the shebang line. For windows it should read:
#!/perl/bin/perl
On the upside, I learned a ton about Apache (gotta stay positive)
Works swimmingly now
Tony (August 28, 2005 at 7:21 pm)
John-
Spectacular!!!
Thank you.
P.S. If you want to get rid of mutliple "markers" when geocoding more than one address, simply change the address() method as so:
function address() {
map.clearOverlays();
GAddress( document.getElementById("a").value, move );
return false;
}
Doug (September 1, 2005 at 10:37 am)
Just FYI an alternative approach that I've been using is querying a Google Maps page to generate lat/long values for addresses.
If you query: http://maps.google.com/maps?oi=map&output=js&q=ADDRESS-GOES-HERE
you get a small HTML page that contains the lat/long that Google calculates. I've noticed this is slightly different sometimes than the geocoder values.
The returned page contains a line like this: , so you just have to extract the values (I use PHP but you could use perl, etc).
The reason I like this more than using geocoder is that it is more flexible in terms of what addresses it can handle. For example, instead of writing out "San Francisco, CA" I can just write "sf, ca".
Hopefully Google won't get up in a fuss about using them for geolocation.
Doug (September 1, 2005 at 10:39 am)
woops, a line of code in the last post got taken out, the pertienent line that Google returns is: <center lat="LATITDUE" lngLONGITUDE" />
David (September 5, 2005 at 9:25 am)
To clean up the google key issue, try adding
key="
vice
key=CHANGEME"
Change the name of the of index.html, index.php
Create new file key.php
If create a number of site using your code you can create on your test site and migrate files without concern for map_key issues.
PS John nice clean code!
JaMeZ (September 16, 2005 at 2:23 pm)
Hello,
I have a situation where I need to find the county the address is as well. Is this possible? Mapping addresses to counties on a map?
thanks,
Jared (November 1, 2005 at 1:17 pm)
Hi, I have a similar want as Dana above in post 18. I would like to display the map in the page along with other information. The address is stored in a mySQL database, and I already have functions that will pull it out.
What I want to do is just call up a map that shows the address of the house on the page, without the form. Is it possible to call your function with a predefined address? As in, no form to submit, it calls the function when the page loads.
slim79 (November 13, 2005 at 1:17 am)
I would like to be able to take multiple addresses from excel or a csv file and map them to a google map is there an easy way to do this?