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 search for locations on a map.
You can search for businesses on a map.
You can get driving directions between two points.
You can make your own map now with the My Maps feature.
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]
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:
What do the various parameters in the URL mean? Table 2-4 draws from the Google Maps Parameters page of the Mapki wiki.[36]
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 | sll contains 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]
mrad
lets you specify an additional destination address.
output=kml
gets a KML file to send to Google Earth.
layer=t
adds the traffic layer.
mrt=kmlkmz
shows “user-created content.” For example, the following shows user-generated information about hotels around the White House:
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.
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:
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}
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:
You can get KML output from Yahoo! Pipes from the following:
which you can feed into Google Maps in the q={URL-of-KML}
parameter:
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:
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).”
[36] http://mapki.com/wiki/Google_Map_Parameters
, accessed as http://mapki.com/index.php?title=Google_Map_Parameters&oldid=4145
[37] http://mapki.com/wiki/Google_Map_Parameters
, accessed as http://maps.google.com/?maps?f=?q&hl=?en&q=1600+Pennsylvania+Ave,+Washington,+DC
on April 14, 2007