Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Beatnik (493)

  (email not shown publicly)

A 29 year old belgian who likes Mountain Dew, Girl Scout Cookies, Tim Hortons French Vanilla Flavoured Cappucinno, Belgian beer, Belgian chocolate, Belgian women, Magners Cider, chocolate chipped cookies and Perl. Likes snowboarding, snorkling, sailing and silence. Bach can really cheer him up! He still misses his dog.

Project Daddy of Spine [], a mod_perl based CMS.

In his superhero time (8.30 AM to 5.30 PM), he works on world peace.

Journal of Beatnik (493)

Tuesday November 21, 2006
06:14 AM

Google Maps Geocodes

[ #31673 ]
I'm playing with Google Maps (this Javascript stuff isn't all THAT hard!). It turns out most of the mapping is done with coordinates (go figure) but I couldn't find a quick way to look up the geocodes for a specific address. I browsed around on the Google Maps API page and found a nice example I could abuse (I can't seem to find the example anymore.. perhaps it's in a wiki somewhere). I adapted the code so it also displays the correct longitude and latitude of the address. I store this information in an XML file and load it with the GXML class in the API. The following HTML/Javascript works for me:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src=";v=2.x&amp;key=USE-YOUR-OWN-KEY-HER E"
    <script type="text/javascript">

    var map;
    var geocoder;

    function load() {
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GSmallMapControl());
      map.setCenter(new GLatLng(34, 0), 1);
      geocoder = new GClientGeocoder();

    // addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
    function addAddressToMap(response) {
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
      } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
        marker = new GMarker(point);
        marker.openInfoWindowHtml(place.address + '<br>' +
          '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode+'<br>1:'+place.Point.coordinates[1] +'<br>0:'+place.Point.coordinates[0]);

    // showLocation() is called when you click on the Search button
    // in the form.  It geocodes the address entered into the form
    // and adds a marker to the map at that location.
    function showLocation() {
      var address = document.forms[0].q.value;
      geocoder.getLocations(address, addAddressToMap);

   // findLocation() is used to enter the sample addresses into the form.
    function findLocation(address) {
      document.forms[0].q.value = address;

  <body onload="load()">

    <!-- Creates a simple input box where you can enter an address
         and a Search button that submits the form. //-->
    <form action="#" onsubmit="showLocation(); return false;">
        <b>Search for an address:</b>
        <input type="text" name="q" value="" class="address_input" size="40" />
        <input type="submit" name="find" value="Search" />

    <div id="map" style="width: 500px; height: 300px"></div>

   <!-- Sample addresses //-->
   <p><b>Try these:</b><br />

   <a href="javascript:void(0)"
     onclick="findLocation('43 Church Street, Parry Sound, ON P2A 1Y6, Canada');return false;">43 Church Street, Parry Sound, ON P2A 1Y6, Canada</a><br />


As an example address, I used a nice bed and breakfast I stayed a few years ago. See it in action here.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • > I couldn't find a quick way to look up the geocodes for a specific address. [] service [NonCommercial-ShareAlike and Commercial licenses] [] engine library [Perl GPL+Artistic]
    using same public USG database as everyone else.

    # I had a sig when sigs were cool
    use Sig;
    • The only problem is that I'm looking up locations in Canada eh.. Both solutions won't work to find geocodes there :(
      • Ah. yes, TigerLine data assumes 99999-9999 postal codes not 9A9-A9A or vice versa ... and doesn't include the frozen provinces to the north.
        # I had a sig when sigs were cool
        use Sig;