Ensure zoom is within span when adding a layer with min/maxzoom, fixes #4915 (#4916)

* Ensure zoom is within span when adding a layer with min/maxzoom, fixes #4915.

* Add tests written by @theashyster

* Add tests to verify map's zoom is actually adjusted to layer's min/max
This commit is contained in:
Iván Sánchez Ortega 2016-11-11 10:46:31 +01:00 committed by Per Liedman
parent d5c499b732
commit 8c22c4e385
2 changed files with 35 additions and 0 deletions

View File

@ -281,6 +281,34 @@ describe("Map", function () {
expect(map.getMinZoom()).to.be(2); expect(map.getMinZoom()).to.be(2);
expect(map.getMaxZoom()).to.be(20); expect(map.getMaxZoom()).to.be(20);
}); });
it("layer minZoom overrides map zoom if map has no minZoom set and layer minZoom is bigger than map zoom", function () {
var map = L.map(document.createElement("div"), {zoom: 10});
L.tileLayer("{z}{x}{y}", {minZoom: 15}).addTo(map);
expect(map.getMinZoom()).to.be(15);
});
it("layer maxZoom overrides map zoom if map has no maxZoom set and layer maxZoom is smaller than map zoom", function () {
var map = L.map(document.createElement("div"), {zoom: 20});
L.tileLayer("{z}{x}{y}", {maxZoom: 15}).addTo(map);
expect(map.getMaxZoom()).to.be(15);
});
it("map's zoom is adjusted to layer's minZoom even if initialized with smaller value", function () {
var map = L.map(document.createElement("div"), {zoom: 10});
L.tileLayer("{z}{x}{y}", {minZoom: 15}).addTo(map);
expect(map.getZoom()).to.be(15);
});
it("map's zoom is adjusted to layer's maxZoom even if initialized with larger value", function () {
var map = L.map(document.createElement("div"), {zoom: 20});
L.tileLayer("{z}{x}{y}", {maxZoom: 15}).addTo(map);
expect(map.getZoom()).to.be(15);
});
}); });
describe("#hasLayer", function () { describe("#hasLayer", function () {

View File

@ -260,5 +260,12 @@ L.Map.include({
if (oldZoomSpan !== this._getZoomSpan()) { if (oldZoomSpan !== this._getZoomSpan()) {
this.fire('zoomlevelschange'); this.fire('zoomlevelschange');
} }
if (this.options.maxZoom === undefined && this._layersMaxZoom && this.getZoom() > this._layersMaxZoom) {
this.setZoom(this._layersMaxZoom);
}
if (this.options.minZoom === undefined && this._layersMinZoom && this.getZoom() < this._layersMinZoom) {
this.setZoom(this._layersMinZoom);
}
} }
}); });