Merge pull request #524 from ahocevar/panzoombar

Zoom level restriction improvements. r=@bartvde
This commit is contained in:
ahocevar
2012-06-22 16:59:13 -07:00
4 changed files with 77 additions and 19 deletions

View File

@@ -99,6 +99,7 @@ OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, {
this.map.events.un({
"changebaselayer": this.redraw,
"updatesize": this.redraw,
scope: this
});
@@ -116,7 +117,11 @@ OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, {
*/
setMap: function(map) {
OpenLayers.Control.PanZoom.prototype.setMap.apply(this, arguments);
this.map.events.register("changebaselayer", this, this.redraw);
this.map.events.on({
"changebaselayer": this.redraw,
"updatesize": this.redraw,
scope: this
});
},
/**
@@ -189,6 +194,7 @@ OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, {
_addZoomBar:function(centered) {
var imgLocation = OpenLayers.Util.getImageLocation("slider.png");
var id = this.id + "_" + this.map.id;
var minZoom = this.map.getMinZoom();
var zoomsToEnd = this.map.getNumZoomLevels() - 1 - this.map.getZoom();
var slider = OpenLayers.Util.createAlphaImageDiv(id,
centered.add(-1, zoomsToEnd * this.zoomStopHeight),
@@ -211,7 +217,7 @@ OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, {
var sz = {
w: this.zoomStopWidth,
h: this.zoomStopHeight * this.map.getNumZoomLevels()
h: this.zoomStopHeight * (this.map.getNumZoomLevels() - minZoom)
};
var imgLocation = OpenLayers.Util.getImageLocation("zoombar.png");
var div = null;
@@ -242,7 +248,7 @@ OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, {
this.map.events.register("zoomend", this, this.moveZoomBar);
centered = centered.add(0,
this.zoomStopHeight * this.map.getNumZoomLevels());
this.zoomStopHeight * (this.map.getNumZoomLevels() - minZoom));
return centered;
},

View File

@@ -83,6 +83,7 @@ OpenLayers.Map = OpenLayers.Class({
* mouseout - triggered after mouseout the map
* mousemove - triggered after mousemove the map
* changebaselayer - triggered after the base layer changes
* updatesize - triggered after the <updateSize> method was executed
*/
/**
@@ -1490,6 +1491,7 @@ OpenLayers.Map = OpenLayers.Class({
}
}
this.events.triggerEvent("updatesize");
},
/**
@@ -1784,18 +1786,42 @@ OpenLayers.Map = OpenLayers.Class({
* <baseLayer>'s maxExtent.
*/
adjustZoom: function(zoom) {
var resolution, resolutions = this.baseLayer.resolutions,
maxResolution = this.getMaxExtent().getWidth() / this.size.w;
if (this.getResolutionForZoom(zoom) > maxResolution) {
for (var i=zoom|0, ii=resolutions.length; i<ii; ++i) {
if (resolutions[i] <= maxResolution) {
zoom = i;
break;
}
if (this.baseLayer && this.baseLayer.wrapDateLine) {
var resolution, resolutions = this.baseLayer.resolutions,
maxResolution = this.getMaxExtent().getWidth() / this.size.w;
if (this.getResolutionForZoom(zoom) > maxResolution) {
if (this.fractionalZoom) {
zoom = this.getZoomForResolution(maxResolution);
} else {
for (var i=zoom|0, ii=resolutions.length; i<ii; ++i) {
if (resolutions[i] <= maxResolution) {
zoom = i;
break;
}
}
}
}
}
return zoom;
},
/**
* APIMethod: getMinZoom
* Returns the minimum zoom level for the current map view. If the base
* layer is configured with <wrapDateLine> set to true, this will be the
* first zoom level that shows no more than one world width in the current
* map viewport. Components that rely on this value (e.g. zoom sliders)
* should also listen to the map's "updatesize" event and call this method
* in the "updatesize" listener.
*
* Returns:
* {Number} Minimum zoom level that shows a map not wider than its
* <baseLayer>'s maxExtent. This is an Integer value, unless the map is
* configured with <fractionalZoom> set to true.
*/
getMinZoom: function() {
return this.adjustZoom(0);
},
/**
* Method: moveTo
@@ -1818,13 +1844,11 @@ OpenLayers.Map = OpenLayers.Class({
zoom = Math.round(zoom);
}
}
if (this.baseLayer.wrapDateLine) {
var requestedZoom = zoom;
zoom = this.adjustZoom(zoom);
if (zoom !== requestedZoom) {
// zoom was adjusted, so keep old lonlat to avoid panning
lonlat = this.getCenter();
}
var requestedZoom = zoom;
zoom = this.adjustZoom(zoom);
if (zoom !== requestedZoom) {
// zoom was adjusted, so keep old lonlat to avoid panning
lonlat = this.getCenter();
}
// dragging is false by default
var dragging = options.dragging || this.dragging;