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:
@@ -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", {
|
||||
|
||||
Reference in New Issue
Block a user