Calling Google Maps by Address

the coolest shit

Interest in embedding Google Maps (gmaps) into web pages and blogs is at an all time high. The Google Maps API is probably the most popular API the company has ever published. In spite of the API’s popularity, it’s basic use (as a means to define and display graphical map data) has been troubled by the difficulties of geocoding.

In a nutshell, geocoding is translating street address data into coordinates of latitude and longitude. This is necessary because the calls to the Google Maps API you have to make from your own web site have to call the map you want to display by the coordinates of the map’s desired centerpoint.

The javascript needed to call the map for a specific address might look like this:

var map = new GMap2(document.getElementById(”map”));
map.addControl(new GSmallMapControl());
map.setCenter(new GLatLng(40.752041, -73.868076), 13);
var marker = new GMarker(new GLatLng(40.752041, -73.868076));
map.addOverlay(marker);
GEvent.addListener(marker, “click”, function() {
marker.openInfoWindowHtml(WINDOW_HTML);
});
marker.openInfoWindowHtml(WINDOW_HTML);
}

Since most of us have never found it necessary to translate conventional address data (address1, address2, city, state, zip) into coordinates, the need to geocode has become practically synonymous with the desire to deploy Google Maps applications. If you have a large database of addresses, how you approach the geocoding problem could have a significant impact on the functioning and performance of your application. The basic question is do you geocode all your address data en masse and create Google Maps calls based on the resulting coordinates? Or, do you interpose a method of geocoding between your application and the API, such that addresses are converted to latitude and longitude each time the needed map is called? The latter is the sexier approach, and has attracted some clever solutions. One example makes use of the free geocoding service provided at geocoder.us, which comes in XML-RPC, REST and SOAP flavors.

First, you have to transmit the street address to the geocoder service like this (SOAP example):

< ?p hp
// Create new instance of the SoapClient class
// You'll need to download the WSDL file from:
// http://geocoder.us/dist/eg/clients/GeoCoder.wsdl
$client = new SoapClient("geocoder.wsdl");
// Retrieve the address coordinates
$result = $client->geocode(”37-12 99th St, Corona, NY”);
// Dump the returned object
var_dump($result);
?>

This query would return something like this:

array(1) { [0]=>
object(stdClass)#1 (10) {
[”number”]=> int(411)
[”zip”]=> int(11368)
[”suffix”]=> string(0) “”
[”prefix”]=> string(0) “”
[”type”]=> string(2) “St”
[”street”]=> string(11) “99th”
[”state”]=> string(2) “NY”
[”city”]=> string(8) “Corona”
[”lat”]=> float(40.752041)
[”long”]=> float(-73.868076)
}

Once the street address had been converted to coordinates, you would be ready to transmit them to the Google Maps API. You could do this using a simple script like this: google maps by address code

This works, but not as well as a system that allowed for the Google Map to be called directly by its address. The Google AJAX Search API seems to provide such solution. It’s called GSmapSearchControl and it lets you call your maps using address data, rather than latitude and longitude, using an onload handler like this:

< script type="text/javascript">
function OnLoad() {
new GSmapSearchControl(
document.getElementById(”mapsearch”), // container
“37-12 99th St, Corona, NY”, // center point
null // options
);
}

The AJAX Local Search API also provides for extensive manipulation of the map controls. Additional code examples are provided here.

One Response to “Calling Google Maps by Address”

  1. everything blog » Blog Archive » WSDL Geocoder Client Says:

    […] You’ll need to download the WSDL file from:
    http://geocoder.us/dist/eg/clients/GeoCoder.wsdl […]

Leave a Reply