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
This commit is contained in:
Tim Schaub
2010-02-03 17:09:17 +00:00
parent af1e754546
commit a5848fc84d
+14 -22
View File
@@ -1098,16 +1098,18 @@ OpenLayers.Map = OpenLayers.Class({
* newBaseLayer - {<OpenLayers.Layer>} * newBaseLayer - {<OpenLayers.Layer>}
*/ */
setBaseLayer: function(newBaseLayer) { setBaseLayer: function(newBaseLayer) {
var oldExtent = null;
if (this.baseLayer) {
oldExtent = this.baseLayer.getExtent();
}
if (newBaseLayer != this.baseLayer) { if (newBaseLayer != this.baseLayer) {
// is newBaseLayer an already loaded layer?m // ensure newBaseLayer is already loaded
if (OpenLayers.Util.indexOf(this.layers, newBaseLayer) != -1) { 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 // make the old base layer invisible
if (this.baseLayer != null && !this.allOverlays) { if (this.baseLayer != null && !this.allOverlays) {
this.baseLayer.setVisibility(false); this.baseLayer.setVisibility(false);
@@ -1124,24 +1126,14 @@ OpenLayers.Map = OpenLayers.Class({
this.baseLayer.setVisibility(true); this.baseLayer.setVisibility(true);
} }
//redraw all layers // recenter the map
var center = this.getCenter();
if (center != null) { if (center != null) {
// new zoom level derived from old scale
//either get the center from the old Extent or just from var newZoom = this.getZoomForResolution(
// the current center of the map. newResolution || this.resolution, true
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);
// zoom and force zoom change // zoom and force zoom change
this.setCenter(newCenter, newZoom, false, true); this.setCenter(center, newZoom, false, true);
} }
this.events.triggerEvent("changebaselayer", { this.events.triggerEvent("changebaselayer", {