move zoom restricting logic to GridLayer

This commit is contained in:
Vladimir Agafonkin 2013-12-05 20:27:08 +02:00
parent 4011a6199f
commit 36b8c7cf4d
3 changed files with 32 additions and 20 deletions

View File

@ -13,10 +13,8 @@ L.Layer = L.Class.extend({
if (map._layers[id]) { return this; }
map._layers[id] = this;
// TODO getMaxZoom, getMinZoom in ILayer (instead of options)
if (this.options && (!isNaN(this.options.maxZoom) || !isNaN(this.options.minZoom))) {
map._zoomBoundLayers[id] = this;
map._updateZoomLevels();
if (this.beforeAdd) {
this.beforeAdd(map);
}
map.whenReady(this._layerAdd, this);
@ -45,11 +43,6 @@ L.Layer = L.Class.extend({
if (map._loaded) {
map.fire('layerremove', {layer: this});
}
if (map._zoomBoundLayers[id]) {
delete map._zoomBoundLayers[id];
map._updateZoomLevels();
}
},
getPane: function () {

View File

@ -44,11 +44,16 @@ L.GridLayer = L.Layer.extend({
this._update();
},
beforeAdd: function (map) {
map._addZoomLimit(this);
},
onRemove: function (map) {
this._clearBgBuffer();
L.DomUtil.remove(this._container);
map.off(this._getEvents(), this);
map._removeZoomLimit(this);
this._container = this._map = null;
},

View File

@ -544,23 +544,37 @@ L.Map = L.Class.extend({
return this.getMaxZoom() - this.getMinZoom();
},
_addZoomLimit: function (layer) {
if (isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom)) {
this._zoomBoundLayers[L.stamp(layer)] = layer;
this._updateZoomLevels();
}
},
_removeZoomLimit: function (layer) {
var id = L.stamp(layer);
if (this._zoomBoundLayers[id]) {
delete this._zoomBoundLayers[id];
this._updateZoomLevels();
}
},
_updateZoomLevels: function () {
var i,
minZoom = Infinity,
var minZoom = Infinity,
maxZoom = -Infinity,
oldZoomSpan = this._getZoomSpan();
oldZoomSpan = this._getZoomSpan(),
i;
for (i in this._zoomBoundLayers) {
var layer = this._zoomBoundLayers[i];
if (!isNaN(layer.options.minZoom)) {
minZoom = Math.min(minZoom, layer.options.minZoom);
}
if (!isNaN(layer.options.maxZoom)) {
maxZoom = Math.max(maxZoom, layer.options.maxZoom);
}
var options = this._zoomBoundLayers[i].options;
minZoom = isNaN(options.minZoom) ? minZoom : Math.min(minZoom, options.minZoom);
maxZoom = isNaN(options.maxZoom) ? maxZoom : Math.max(maxZoom, options.maxZoom);
}
if (i === undefined) { // we have no tilelayers
if (i === undefined) {
// we have no zoom restricting layers
this._layersMaxZoom = this._layersMinZoom = undefined;
} else {
this._layersMaxZoom = maxZoom;