diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index bd8378a10b..e545113d2a 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -1098,16 +1098,18 @@ OpenLayers.Map = OpenLayers.Class({ * newBaseLayer - {} */ setBaseLayer: function(newBaseLayer) { - var oldExtent = null; - if (this.baseLayer) { - oldExtent = this.baseLayer.getExtent(); - } - + if (newBaseLayer != this.baseLayer) { - // is newBaseLayer an already loaded layer?m + // ensure newBaseLayer is already loaded if (OpenLayers.Util.indexOf(this.layers, newBaseLayer) != -1) { + // preserve center and scale when changing base layers + var center = this.getCenter(); + var newResolution = OpenLayers.Util.getResolutionFromScale( + this.getScale(), newBaseLayer.units + ); + // make the old base layer invisible if (this.baseLayer != null && !this.allOverlays) { this.baseLayer.setVisibility(false); @@ -1124,24 +1126,14 @@ OpenLayers.Map = OpenLayers.Class({ this.baseLayer.setVisibility(true); } - //redraw all layers - var center = this.getCenter(); + // recenter the map if (center != null) { - - //either get the center from the old Extent or just from - // the current center of the map. - var newCenter = (oldExtent) - ? oldExtent.getCenterLonLat() - : center; - - //the new zoom will either come from the old Extent or - // from the current resolution of the map - var newZoom = (oldExtent) - ? this.getZoomForExtent(oldExtent, true) - : this.getZoomForResolution(this.resolution, true); - + // new zoom level derived from old scale + var newZoom = this.getZoomForResolution( + newResolution || this.resolution, true + ); // zoom and force zoom change - this.setCenter(newCenter, newZoom, false, true); + this.setCenter(center, newZoom, false, true); } this.events.triggerEvent("changebaselayer", {