Learning Google Maps

Just as there are different UI elements packaged up in one of the major JavaScript libraries, vendors have already started to create reusable JavaScript components. The most famous of these Ajax widgets is Google Maps. In this section, we’ll look at how to embed a Google map using the Google Maps API. The online documentation on how to get started with the maps at the Google web site is good.[117]

Here we will set up a simple map and then use the JavaScript Shell to work with a live map so that you can invoke a command and see an immediate response. The intended effect is that you see the widgets as dynamic programs that respond to commands, whether that command comes in a program or from you entering that command.

Use the Google Maps API to make a simple map:

  1. Make sure you have a public web directory to host your map and know the URL of that directory. Any Google map that uses the free, public API needs to be publicly visible.

  2. Go to the sign-­up page for a key to access Google Maps.[118]

  3. Read the terms of service,[119]http://examples.mashupguide.net/ch08. Write down the resultant key.

  4. Copy and paste the HTML code into your own page in your web-­hosting directory. You should get something like the following (except that the code will have your API key):[120]

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    <html xmlns="http://www.w3.org/1999/xhtml">
        <meta ?http-?equiv="content-type" content="text/html; charset=utf-8"/>
        <title>Google Maps JavaScript API Example</title>
        <script type="text/JavaScript">
        function load() {
          if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map"));
            map.setCenter(new GLatLng(37.4419, -122.1419), 13);
      <body onload="load()" onunload="GUnload()">
        <div id="map" style="width: 500px; height: 300px"></div>
  5. Now make one modification to the example by removing the var keyword in front of map to make it a global variable that is thus accessible to the JavaScript Shell. That is, change the following:

    var map = new GMap2(document.getElementById("map"));
    to map = new GMap2(document.getElementById("map"));

    to expose the map object to the JavaScript Shell utility.[121]

  6. Invoke the JavaScript Shell for your map by hitting the JavaScript Shell bookmarklet in the context of your map. Type the following code fragments, and see what happens. (Note that another approach is to modify your code directly with these code fragments and reload your page.) One can correlate the actions to the documentation for version 2 of the Google Maps API.[122]

To return the current zoom level of the map (which goes from 0 to 17, with 17 for the most detailed), use this:



To obtain the latitude and longitude of the center of the map, enter the following:


(37.4419, -122.1419)

To center the map around the Campanile for UC Berkeley, use this:

map.setCenter(new GLatLng(37.872035,-122.257844), 13);

You can pan to that location instead:

map.panTo(new GLatLng(37.872035,-122.257844));

To add a small map control (to control the zoom level), use the following:

map.addControl(new GSmallMapControl())
map.addControl(new GMapTypeControl())

To add GMap keyboard navigation so that you can pan and zoom with the keyboard, use this:

window.kh = new GKeyboardHandler(map)

[object Object]

To fully zoom out the map, use this:


To zoom in all the way, use the following:


To set the variable maptypes to an array holding three objects, use this:

maptypes = map.getMapTypes()

[object Object],[object Object],[object Object]

To get the name of the first entry in maptypes (1 corresponds to satellite, while 2 corresponds to the hybrid map type):



To get the current map type, you can get the object and the name of that type object:


[object Object]



To set the map type to satellite, use the following:


You can zoom one level in and out if you are not already at the max or min zoom level:


To make an overlay, try the following:

point = new GLatLng (37.87309185260284, -122.25508689880371)

(37.87309185260284, -122.25508689880371)

marker = new GMarker(point)

[object Object]


To make something happen when you click the marker, type the following:

GEvent.addListener(marker, 'click', function() { 
  marker.openInfoWindowHtml('hello'); })

[object Object]

There are many more features to explore, such as polylines, overlays, and draggable points. To learn more, I certainly recommend the “Google Maps API: Introduction” documentation.[123]