A common task in using online maps is to geocode addresses—that is, converting street addresses to the corresponding latitude and longitude. In the following sections, I will walk through the basics of geocoding in Yahoo!, Google,, and Virtual Earth maps, which is enough to get you started. For the following examples, I use the address of Apress, which is 2855 Telegraph Ave., Berkeley, CA.


There are subtleties that I won’t go in detail about: the precision and accuracy of the APIs, dealing with ambiguities in the addresses, and which geocoder is best for a given geographic location.

Yahoo! Maps

Yahoo! provides a REST geocoding method here:

whose base URL is and whose parameters include the appid to identify your application and two ways of identifying the address:

  • A combination of street, city, state, and zip:{app-id}&street=2855+Telegraph+Ave.&city=Berkeley&state=CA
  • location, which is free text that consists of one string that holds a combination of street, city, state, and zip. The location string has priority for determining the placement of this:{app-id}&location=2855+Telegraph+Ave.%2C+Berkeley%2C+CA

When you use these two methods to geocode the location of the Apress office, you get the same result. Using street, city, and state returns this:

<?xml version="1.0"?>
<ResultSet xmlns:xsi="" 
  <Result precision="address">
    <Address>2855 TELEGRAPH AVE</Address>

Note the following characteristics of the output:

  • You get the same results with the two methods in this case.

  • You can compare the output address and the input address to make sure the geocoder is interpreting the address the way you think it should be.

  • The default output format is XML when no output parameter is specified.

  • You get the latitude and longitude in the <Latitude> and <Longitude> elements, respectively.

A good way to see how the API behaves is to try various parameters. See what happens in the following cases:

  • Specify only city=Berkeley to get several results corresponding to the cities that go by the name of Berkeley.[202]

  • Use the output=php option to get serialized PHP.[203]

  • Enter a nonexistent street address for a given city.[204] provides a free gecoding service for U.S. addresses. Refer to Chapter 7 for a detailed discussion of the REST and SOAP interfaces to the API, which is documented here:

Let’s calculate the latitude and longitude for the Apress office with different aspects of the service:

Notice the use of the W3 Basic Geo encoding for the latitude and longitude in the response.

Google Geocoder

The Google Geocoder provides two interfaces: a REST interface and a JavaScript-­accessible interface:

I’ll cover each in turn.

REST Interface

We’ll first look at the REST method, whose base URL is and whose parameters are as follows:

  • q is the address to geocode.

  • key is your API key.[205]

  • output is the format of the output—one of xml, kml, csv, or json.

Let’s look at some example output. The xml and kml output for 2855 Telegraph Ave., Berkeley, CA, produces the same body (listed next) but different Content-Type headers (“text/xml” and “application/”), respectively:[206]

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="">
    <name>2855 Telegraph Ave., Berkeley, CA.</name>
    <Placemark id="p1">
      <address>2855 Telegraph Ave, Berkeley, CA 94705, USA</address>
      <AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
                  <ThoroughfareName>2855 Telegraph Ave</ThoroughfareName>

Even though the XML output does include a </Placemark> element, which is valid KML, the </Response> element in the output precludes the output from being valid KML.

Here is the json output for convenient use in JavaScript:[207]

{"name":"2855 Telegraph Ave., Berkeley, CA.","Status":{"code":200,"request":
"geocode"},"Placemark":[{"id":"p1","address":"2855 Telegraph Ave, Berkeley, 
CA 94705, USA","AddressDetails":{"Country":{"CountryNameCode":"US",
"Thoroughfare":{"ThoroughfareName":"2855 Telegraph Ave"},"PostalCode":
{"PostalCodeNumber":"94705"}}}}},"Accuracy": 8},"Point":{"coordinates":

JavaScript Interface

As for the JavaScript methods available in the Google mapping system, consult the documentation provided by Google,[208]GClientGeocoder object. You can use the JavaScript Shell to see this object in action:

  1. Open the simple Google map from the previous section.[209]

  2. In the JavaScript Shell, set up the address and an instance of GClientGeocoder:

    address = "2855 Telegraph Ave., Berkeley, CA"

    2855 Telegraph Ave., Berkeley, CA

    geocoder = new GClientGeocoder();

    [object Object]

  3. Compose a call to GClientGeocoder.getLatLng, which takes an address and a callback function, which in turn takes the point geocoded from the address:

    geocoder.getLatLng(address, function(point) { 
       if (!point) {
         alert(address + " not found");}
       else {
         map.setCenter(point, 13);
         var marker = new GMarker(point);
  4. If you get rid of the whitespace in the function, you can easily invoke it in the JavaScript Shell:

    geocoder.getLatLng( address, function(point) { 
      if (!point) { alert(address + " not found");} 
      else { 
        map.setCenter(point, 13); 
        var marker = new GMarker(point); 
    } );
  5. You will then see that Google Maps adds an overlay marking 2855 Telegraph Ave., Berkeley, CA (as shown in Figure 13-1).

Virtual Earth provides geocoding functionality in the VEMap.Find method of version 5 of the Virtual Earth SDK,[210]

  1. Open the basic Virtual Earth example.[211]

  2. Let’s create a callback function that pops up an alert with the latitude/longitude of the found locations:

    function onFoundResults (ShapeLayer,FindResult,Place,HasMore) {
      html = "";
      for (x=0; x<Place.length; x++) {
        html = html + Place[x].LatLong + "";
      alert (html);
  3. In the JavaScript Shell, type the following:

    address = "2855 Telegraph Ave., Berkeley, CA"

    2855 Telegraph Ave., Berkeley, CA

    function onFoundResults (ShapeLayer,FindResult,Place,HasMore) {
      html = ""; 
      for (x=0; x<Place.length; x++) {html = html + Place[x].LatLong + "";}
      alert (html);
    map.Find(null,address,null,null,null,null,null,null,null,null, onFoundResults);

    The last line with the many null parameters might look surprising, but it is an officially recommended invocation from

  4. You will see an alert that pops up the latitude and longitude of the address.

    I packaged this logic in a simple example:[212]

        <title>VE Map showing VEMap.Find (</title>
        <meta ?http-?equiv="Content-Type" content="text/html; charset=utf-8">
        <script src=""></script>
        var map = null;
        function onFoundResults (ShapeLayer,FindResult,Place,HasMore) {
          html = "";
          //alert("Place: " + Place);
          for (x=0; x<Place.length; x++) {
            html = html + Place[x].LatLong + "";
          alert (html);
        function GetMap()
           map = new VEMap('myMap');
           address = "2855 Telegraph Ave., Berkeley, CA";
                    null, onFoundResults);
      <body onload="GetMap();">
        <div id='myMap' style="position:relative; width:400px; height:400px;"></div>

Geocoding Non-­U.S. Addresses

As I have implied in my previous examples, the Google, Yahoo!, Microsoft, and geocoders are built to handle American addresses. What if you want to geocode street-­level addresses in other countries? It would be great to have a single geocoding API that would return a reliable latitude and longitude for an address from anywhere in the world. That service doesn’t currently exist. Here, however, are some starting points to geocoding addresses from around the world:




[205] You can get your key at ABQIAAAAdjiS7YH6Pzk2Nrli02b5xxR10RG5t-vK3TwPKbpNUO2c5sYb4RTmySs_TEFzYvlZrCaYJKlmTzJ5lA is the key for