Google Maps API Tutorial© 2007, 2008, 2009 Mike Williams |
Translate this page: |
Modularized Overlays and ControlsVersion information:
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. GOverviewMapControlIn 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 WindowIn 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. |