OpenLayers OpenLayers

Google Layer


How To Include a Google Maps Layer in OpenLayers

For a working example of a Google Maps Layer in OpenLayers, see examples/google.html, running live from the repository.

  • Create API Key
    The first thing you need to do is to create a unique GMaps API Key for your site. You can create the key on the Google Maps API Key Sign-Up Page.
  • Include GMaps Script
    Once you have created your GMaps API key, you are ready to include the gmaps script on your page. In your HTML page, just before you include the OpenLayers.js script, you need to include the gmaps script, with your GMaps API key set as the "key" parameter.
    For example, if your GMaps API key is: abcdefg1234 then you would include the following:
        <script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg1234'></script>
        <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
    
  • Create the Google Layer
    Once you have correctly loaded the GMaps script, you can create a new Google Layer and add it to your map. The code should look something like this:
        var map = new OpenLayers.Map("mapDiv"); 
        var gmapLayer = new OpenLayers.Layer.Google("GMaps");
        map.addLayer(gmapLayer);