Google Maps

Now, let’s turn to studying the functionality of Google Maps, located at http://maps.google.com/.

With the standard Google Maps site, you can do the following:

You can also embed a Google Maps “widget” into a web page via JavaScript—using the Google Maps API.[34]

Even though Google Maps is not the most highly trafficked online map site,[35]

URL Language of Google Maps

Understanding the syntax and semantics of URLs in Google Maps will help you better recombine the functionality of the standard Google Maps site. Consider an example: I have an address I want to locate—for instance, the address of the White House (1600 Pennsylvania Ave., Washington, D.C.). I go to Google Maps (http://maps.google.com/) and type 1600 Pennsylvania Ave, Washington, DC into the search box to get a map. I get the URL for the map by examining the “Link to this page” link:

http://maps.google.com/maps?f=q&hl=en&q=1600+Pennsylvania+Ave,+Washington,+DC&sll=36.60585,-121.858956&sspn=0.006313,0.01133&ie=UTF8&z=16&om=1&iwloc=addr

What do the various parameters in the URL mean? Table 2-4 draws from the Google Maps Parameters page of the Mapki wiki.[36]

Table 2.4. Dissecting Parameters for a Link to Google Maps
Parameter Description
f=q The f parameter, which controls the display of the Google Maps form, can be d (for the directions form or l for the local form). Without the f parameter, the default search form is displayed.
hl=en Google Maps supports a limited number of host languages, including en for English and fr for French.
q=1600+Pennsylvania+Ave,+Washington,+DC The value of the q parameter is treated as though it were entered via the query box at http://maps.google.com.
sll=36.60585,-121.858956 sllcontains the latitude and longitude for the center point around which a business search is performed.
spn=0.006313, 0.01133 spn is the approximate latitude/longitude span for the map.
ie=UTF8 ie is the character encoding for the map.
om=1 om determines whether to include an overview map. With om=0 the overview map is closed.
iwloc=addr iwloc controls display options for the info window.

A good way to get a feel for how these parameters function is to change a parameter, add new ones, or drop ones in the sample URL and take a look at the resulting map. For instance, if you have only the q parameter, you would still get a map with some default behavior:

http://maps.google.com/maps?q=1600+Pennsylvania+Ave,+Washington,+DC

That is, the other parameters are not mandatory. Let’s play with the z parameter to adjust the zoom factor:

http://maps.google.com/maps?q=1600+Pennsylvania+Ave,+Washington,+DC&z=0

versus the following:

http://maps.google.com/maps?q=1600+Pennsylvania+Ave,+Washington,+DC&z=17

There is a comprehensive list of Google Maps parameters[37]

Just as you can create mashups involving Flickr by using Flickr’s URL language, you can create mashups with Google Maps by exploiting its URL structures. Let’s consider a few examples.

Viewing KML Files in Google Maps

Many of the popular sources for KML (such as http://earth.google.com/gallery/) assume you will view KML in Google Earth. However, you can display a limited subset of KML in Google Maps. Consider, for instance, the KML file at the following location:

http://services.google.com/earth/kmz/global_heritage_fund_n.kmz

It can be viewed in Google Maps by passing in the URL of the KML file via the q parameter, as shown here:

http://maps.google.com/maps?q=http:%2F%2Fservices.google.com%2Fearth%2Fkmz%2Fglobal_heritage_fund_n.kmz

Hence, in your own web site, you can give the option to your users of downloading KML to Google Earth or viewing the KML on Google Maps by linking to the following:

http://maps.google.com/maps?q={URL-of-KML}

Connecting Yahoo! Pipes and Google Maps

A specific case of displaying KML files is feeding KML from Yahoo! Pipes into Google Maps. (I describe Yahoo! Pipes in detail in Chapter 4. For the purposes of this discussion, you need to know only that Yahoo! Pipes can generate KML output.) Consider, for example, Apartment Near Something, configured specifically to list apartments that are close to cafes around UC Berkeley:

http://pipes.yahoo.com/pipes/pipe.info?location=94720&what=cafes&mindist=2&=Run+Pipe&_id=1mrlkB232xGjJDdwXqIxGw&_run=1

You can get KML output from Yahoo! Pipes from the following:

http://pipes.yahoo.com/pipes/pipe.run?_id=1mrlkB232xGjJDdwXqIxGw&_render=kml&_run=1&location=94720&mindist=2&what=cafes

which you can feed into Google Maps in the q={URL-of-KML} parameter:

http://maps.google.com/maps?f=q&hl=en&geocode=&q=http%3A%2F%2Fpipes.yahoo.com%2Fpipes%2Fpipe.run%3F_id%3D1mrlkB232xGjJDdwXqIxGw%26_render%3Dkml%26_run%3D1%26location%3D94720%26mindist%3D2%26what%3Dcafes&ie=UTF8&ll=37.992916,-122.24556&spn=0.189398,0.362549&z=12&om=1

Other Simple Applications of the Google Maps URL Language

Here are a few other examples of how to connect Google Maps to your applications by creating the appropriate URL:

  • Let’s not forget that by just using q={address}, you can now generate a URL to a map centered around that address. If such a map suffices, it’s hard to imagine a simpler way to create a map corresponding to that address. No geocoding is needed.

  • You can create a URL for custom driving directions for any source and destination address creating custom driving directions from your spreadsheet of addresses by making the URLs. For example, to generate driving directions from Apress to the Computer History Museum, you can use this:

    http://www.google.com/maps?saddr={source-address}&daddr={destination-address}

    to generate this:

    http://www.google.com/maps?saddr=2855+Telegraph+Ave,+Berkeley,+CA+94705&daddr=1401+N+Shoreline+Blvd,+Mountain+View,+CA+94043

    Although driving directions have recently been added to the Google Maps API, [38]it is currently not possible to use the API to create directions to avoid highways, something you can do by using the dirflg=h parameter.[39] Hence, you can easily generate a scenic route for myself between the Apress offices and the Computer Museum, while avoiding the API altogether: http://www.google.com/maps?saddr=2855+Telegraph+Ave,+Berkeley,+CA+94705&daddr=1401+N+Shoreline+Blvd,+Mountain+View,+CA+94043&dirflg=h

    It pays to know the URL language of an application!

  • You can use Google Maps as a nonprogrammer’s geocoder. Center the map on the point for which you want to calculate its latitude and longitude, and read the values off the ll parameter. If the ll parameter is not present, you can double-­click the center of the map, just enough to cause the map to recenter on the requested point.



[35] http://news.yahoo.com/s/ap/20070405/ap_on_hi_te/google_maps—“Google’s maps already are a big draw, with 22.2 million U.S. visitors during February, according to the most recent data available from comScore Media Metrix. That ranked Google Maps third in its category, trailing AOL’s Mapquest (45.1 million visitors) and Yahoo (29.1 million visitors).”