Skip to content

Commit

Permalink
Only create Google control once
Browse files Browse the repository at this point in the history
The control div is now an empty container that we can always append to and
remove from without having to worry about GMaps changing styles on it. It
also makes sure that the control is appended before Google's own
attribution control, so the "Report a map error" link will always be
clickable.
  • Loading branch information
ahocevar committed Dec 6, 2012
1 parent d75e3ec commit 978cb4b
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions lib/OpenLayers/Layer/Google/v3.js
Expand Up @@ -88,10 +88,15 @@ OpenLayers.Layer.Google.v3 = {
scrollwheel: false,
streetViewControl: false
});
var googleControl = document.createElement('div');
googleControl.style.width = '100%';
googleControl.style.height = '100%';
mapObject.controls[google.maps.ControlPosition.TOP_LEFT].push(googleControl);

// cache elements for use by any other google layers added to
// this same map
cache = {
googleControl: googleControl,
mapObject: mapObject,
count: 1
};
Expand Down Expand Up @@ -134,32 +139,17 @@ OpenLayers.Layer.Google.v3 = {
}
var container = this.mapObject.getDiv();
if (visible === true) {
if (!cache.gmapHasViewport) {
this.map.div.removeChild(this.map.viewPortDiv);
this.mapObject.controls[google.maps.ControlPosition.TOP_LEFT].push(this.map.viewPortDiv);
cache.gmapHasViewport = true;
if (!cache.googleControl.hasChildNodes()) {
cache.googleControl.appendChild(this.map.viewPortDiv);
this.map.div.appendChild(container);
google.maps.event.trigger(this.mapObject, 'resize');
}
this.mapObject.setMapTypeId(type);
cache.displayed = this.id;
} else {
if (cache.gmapHasViewport) {
if (cache.googleControl.hasChildNodes()) {
this.map.div.removeChild(container);
this.map.div.appendChild(
this.mapObject.controls[google.maps.ControlPosition.TOP_LEFT].pop()
);
delete cache.gmapHasViewport;

// restore all styles of the viewPortDiv, in case GMaps
// changes any. At the time of writing this, it definitely
// changes the position.
OpenLayers.Util.modifyDOMElement(
this.map.viewPortDiv, null, null, null, 'relative',
null, 'hidden'
);
this.map.viewPortDiv.style.width = "100%";
this.map.viewPortDiv.style.height = "100%";
this.map.div.appendChild(this.map.viewPortDiv);
}
delete cache.displayed;
}
Expand Down

0 comments on commit 978cb4b

Please sign in to comment.