Using Google Maps imagery

Google Maps is probably the most known web map application around the world. Their imageries, in the way of tiled layers, are well known by people; they are accustomed to their layer style and because of this you may be interested in using them in your own web mapping project.

Using Google Maps imagery

OpenLayers counts with the OpenLayers.Layer.Google class, which is in fact a wrapper code around the Google Maps API, that allows us to use the Google Maps tiles in a homogeneous way within the OpenLayers API.

Note

Do not confuse Google Maps API with the Google Maps imagery. Google Maps API is a bunch of JavaScript code, which is free to use, while the access to the Google Maps imagery has some usage restrictions and, depending on the number of hits, will be subject to some payments.

How to do it...

To use Google Maps imagery, perform the following steps:

  1. Create an HTML file and add the OpenLayers dependencies.
  2. Include the Google Maps API as follows:
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.5&sensor=false"></script> 
  3. Add a div element to hold the map, as follows:
    <!-- Map DOM element --> 
    <div id="ch2_google" style="width: 100%; height: 100%;"></div> 
  4. Within a script element, add the code to create the map instance and add a layer switcher control, as follows:
    <!-- The magic comes here --> 
    <script type="text/javascript"> 
    
        // Create the map using the specified DOM element 
        var map = new OpenLayers.Map("ch2_google"); 
        map.addControl(new OpenLayers.Control.LayerSwitcher());     
  5. Create some Google based maps and add to the map, as follows:
        var streets = new OpenLayers.Layer.Google("Google Streets", { 
            numZoomLevels: 20 
        }); 
        var physical = new OpenLayers.Layer.Google("Google Physical", { 
            type: google.maps.MapTypeId.TERRAIN 
        }); 
        var hybrid = new OpenLayers.Layer.Google("Google Hybrid", { 
            type: google.maps.MapTypeId.HYBRID, numZoomLevels: 20 
        }); 
        var satellite = new OpenLayers.Layer.Google("Google Satellite", { 
            type: google.maps.MapTypeId.SATELLITE, numZoomLevels: 22 
        }); 
        map.addLayers([physical, streets, hybrid, satellite]); 
    
  6. Finally, center the map on a desired location, as follows:
        map.setCenter(new OpenLayers.LonLat(0, 0), 2); 
    </script>

How it works...

As you can see, the code has three main sections. First we have placed a div element, which will be used for the map, as follows:

<div id="ch2_google" style="width: 100%; height: 100%;"></div> 

Next, we have included the Google Maps API code, as follows:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.5&sensor=false"></script> 

Note

Remember that OpenLayers simply acts as a wrapper, so we need the real Google Maps API code in our application.

Within a <script type="text/javascript"> </script> element, we have added the code necessary to initialize the map and add a layer switcher control, as follows:

    var map = new OpenLayers.Map("ch2_google"); 
    map.addControl(new OpenLayers.Control.LayerSwitcher()); 

Finally, we have added some well known Google Maps layers and centered the map's viewport, as follows:

    var streets = new OpenLayers.Layer.Google("Google Streets", { 
        numZoomLevels: 20 
    }); 
    var physical = new OpenLayers.Layer.Google("Google Physical", { 
        type: google.maps.MapTypeId.TERRAIN 
    }); 
    var hybrid = new OpenLayers.Layer.Google("Google Hybrid", { 
        type: google.maps.MapTypeId.HYBRID, numZoomLevels: 20 
    }); 
    var satellite = new OpenLayers.Layer.Google("Google Satellite", { 
        type: google.maps.MapTypeId.SATELLITE, numZoomLevels: 22 
    }); 
    map.addLayers([physical, streets, hybrid, satellite]); 
    map.setCenter(new OpenLayers.LonLat(0, 0), 2); 

The type of the layers are defined by the Google Maps API class google.maps.MapTypeId, which you can find at http://code.google.com/apis/maps/documentation/javascript/reference.html#MapTypeId.

Note

Note that we are working with two APIs, OpenLayers and Google Maps API, so it would be good to take a look at the Google Maps API to better understand its capabilities.

Documentation can be found at https://developers.google.com/maps/documentation/javascript/tutorial.

There's more...

In this recipe, we have shown you how to use the Google Maps API Version 3 to add the Google imagery to your OpenLayers projects.

For the previous version 2, Google requires you to register as a user and obtain an API key that you need to use to initialize the OpenLayers.Layer.Google instance. The key is later used on every tile request to identify you, so Google can know about your usage.

As you have seen, version 3 is much more simple to use within OpenLayers.

See also

  • The Adding WMS layer recipe
  • The Using Bing imagery recipe
  • The Understanding base and non-base layers recipe in Chapter 1, Web Mapping Basics
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.217.5