From a5848fc84d99a0c339b3a01189f1b27690fb4c3c Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 3 Feb 2010 17:09:17 +0000 Subject: [PATCH] Preserving center and scale when switching base layers. This improves behavior when the map wraps around the domain of the coordinate reference system or when changing between layers in different projections. r=ahocever (closes #2457). git-svn-id: http://svn.openlayers.org/trunk/openlayers@10014 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Map.js | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) 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", {