Google Maps API Tutorial

© 2007, 2008, 2009 Mike Williams

 
Translate this page:

 

Modularized Overlays and Controls

Version information:
  • Info Windows: Google moved the info window back into the main code from v2.135 to v2.183, so this technique is not necessary for those versions.
    Google moved the info window back into an exernal module in v2.184, so this technique is again necessary from v2.184 onwards.
     
  • GOverviewMapControl: The undocumented .getOverviewMap() does not work since v2.135. There is now no way to obtain a reference to the overview map oject.
Google have been moving some code out of the main API code and into external modules. This reduces the memory required to run the API, and the time taken to load the main code. The external modules are only loaded when you make the first call to code that they contain.

The loading of an external module is performed asynchronously. This can be a problem if you were intending to directly modify the contents of something created by that module. Modifying the overlay using API calls is OK. I guess API calls to a module that hasn't finished loading are pipelined.

If you attempt to reference DOM elements of modularized overlays or controls, then you'll find that those elements don't exist for a few hundred milliseconds.

GOverviewMapControl

In v2.93, the code for the map controls was moved into a separate module.

In previous versions of the API you could write

      var overlayControl = new GOverviewMapControl();
      map.addControl(overlayControl);
      var overmap = overlayControl.getOverviewMap();
      var overmapdiv = document.getElementById('map_overview');
From v2.93, those undocumented features no longer work because the GOverviewMapControl doesn't actually exist until a few hundred milliseconds later, after the code that handles it has been loaded. You have to wait for the module to be loaded before those features become accessible. Or, alternatively, you could stop using those undocumented features.

The internal event that is triggered when the code module has been loaded doesn't appear to be accessible, and waiting for a fixed time period carries the risk that it might fail for a user with slower Internet connectivity or when the Google server is busy.

What you could do is wait for a short while, and check to see if the feature is active, and wait again if it isn't. Like this:

      var overlayControl = new GOverviewMapControl();
      map.addControl(overlayControl);
      setTimeout("checkOverview()",100);
   ...
      function checkOverview() {
        overmap = overlayControl.getOverviewMap();
        if (overmap) {      
          ...
        } else {
        setTimeout("checkOverview()",100);
      }
    }
This example logs the availability of .getOverviewMap() and "map_overview".

This example performs the same tweaks as described here for the earlier versions of the API.

Info Window

In v2.123, the code for the info window was moved into a separate module.

In previous versions of the API you could write

     marker.openInfoWindowHtml('<div id="info"> ... </div>');
and immediately access document.getElementById("info").

You might want to do that if you're displaying a mini-map or a StreetView panorama inside the info window.

From v2.123, that doesn't work the first time you open the info window, because the content div doesn't exist until a few hundred milliseconds later, after the code that handles it has been loaded.

If you're opening the info window immediately when the page launches, then you'd have to wait for the module to load, just like the GOverviewMapControl above.

If, however, you only open the info window when the user clicks on a marker, a neater solution is to force the module to be loaded as the page opens, with the expectation that the module will be available by the time that the user clicks on a marker. This can be done like this:

     var map = new GMap2(document.getElementById("map"));
     map.getInfoWindow().show();
The .show() method doesn't display anything, since the info window isn't set up. Other commands that don't display anything are implemented in main.js, and don't cause the module to be loaded.

Back to Mike's Homepage
Up to the start of this tutorial