diff --git a/src/layer/Layer.js b/src/layer/Layer.js index be91af7e..a8a4d792 100644 --- a/src/layer/Layer.js +++ b/src/layer/Layer.js @@ -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 () { diff --git a/src/layer/tile/GridLayer.js b/src/layer/tile/GridLayer.js index 38c670e8..1bdbbd5d 100644 --- a/src/layer/tile/GridLayer.js +++ b/src/layer/tile/GridLayer.js @@ -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; }, diff --git a/src/map/Map.js b/src/map/Map.js index 2a12a302..d18ef685 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -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;