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

View File

@@ -1098,16 +1098,18 @@ OpenLayers.Map = OpenLayers.Class({
* newBaseLayer - {<OpenLayers.Layer>}
*/
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", {