Add minZoom and maxZoom properties to L.CRS as well

This commit is contained in:
Iván Sánchez Ortega
2019-01-25 12:40:41 +01:00
parent 61ef1776ac
commit d7a7ba1b9f
4 changed files with 31 additions and 16 deletions

View File

@ -32,5 +32,7 @@ export var Simple = Util.extend({}, CRS, {
return Math.sqrt(dx * dx + dy * dy); return Math.sqrt(dx * dx + dy * dy);
}, },
infinite: true infinite: true,
minZoom: -Infinity
}); });

View File

@ -135,5 +135,13 @@ export var CRS = {
newNe = new LatLng(ne.lat - latShift, ne.lng - lngShift); newNe = new LatLng(ne.lat - latShift, ne.lng - lngShift);
return new LatLngBounds(newSw, newNe); return new LatLngBounds(newSw, newNe);
} },
// @property minZoom: Number = -Infinity
// The minimum zoom level supported by the CRS.
minZoom: 0,
// @property maxZoom: Number = Infinity
// The maximum zoom level supported by the CRS.
maxZoom: Infinity
}; };

View File

@ -258,8 +258,8 @@ Map.include({
maxZoom = options.maxZoom === undefined ? maxZoom : Math.max(maxZoom, options.maxZoom); maxZoom = options.maxZoom === undefined ? maxZoom : Math.max(maxZoom, options.maxZoom);
} }
this._layersMaxZoom = maxZoom === -Infinity ? undefined : maxZoom; this._layersMaxZoom = isFinite(maxZoom) ? maxZoom : Infinity;
this._layersMinZoom = minZoom === Infinity ? undefined : minZoom; this._layersMinZoom = isFinite(minZoom) ? minZoom : -Infinity;
// @section Map state change events // @section Map state change events
// @event zoomlevelschange: Event // @event zoomlevelschange: Event
@ -269,10 +269,10 @@ Map.include({
this.fire('zoomlevelschange'); this.fire('zoomlevelschange');
} }
if (this.options.maxZoom === undefined && this._layersMaxZoom && this.getZoom() > this._layersMaxZoom) { if (this.getZoom() > this.getMaxZoom()) {
this.setZoom(this._layersMaxZoom); this.setZoom(this._layersMaxZoom);
} }
if (this.options.minZoom === undefined && this._layersMinZoom && this.getZoom() < this._layersMinZoom) { if (this.getZoom() < this.getMinZoom()) {
this.setZoom(this._layersMinZoom); this.setZoom(this._layersMinZoom);
} }
} }

View File

@ -50,13 +50,13 @@ export var Map = Evented.extend({
// Minimum zoom level of the map. // Minimum zoom level of the map.
// If not specified and at least one `GridLayer` or `TileLayer` is in the map, // If not specified and at least one `GridLayer` or `TileLayer` is in the map,
// the lowest of their `minZoom` options will be used instead. // the lowest of their `minZoom` options will be used instead.
minZoom: undefined, minZoom: -Infinity,
// @option maxZoom: Number = * // @option maxZoom: Number = *
// Maximum zoom level of the map. // Maximum zoom level of the map.
// If not specified and at least one `GridLayer` or `TileLayer` is in the map, // If not specified and at least one `GridLayer` or `TileLayer` is in the map,
// the highest of their `maxZoom` options will be used instead. // the highest of their `maxZoom` options will be used instead.
maxZoom: undefined, maxZoom: Infinity,
// @option layers: Layer[] = [] // @option layers: Layer[] = []
// Array of layers that will be added to the map initially // Array of layers that will be added to the map initially
@ -145,6 +145,9 @@ export var Map = Evented.extend({
this.setMaxBounds(options.maxBounds); this.setMaxBounds(options.maxBounds);
} }
this._addLayers(this.options.layers);
this._updateZoomLevels();
if (options.zoom !== undefined) { if (options.zoom !== undefined) {
this._zoom = this._limitZoom(options.zoom); this._zoom = this._limitZoom(options.zoom);
} }
@ -165,8 +168,6 @@ export var Map = Evented.extend({
this._createAnimProxy(); this._createAnimProxy();
DomEvent.on(this._proxy, DomUtil.TRANSITION_END, this._catchTransitionEnd, this); DomEvent.on(this._proxy, DomUtil.TRANSITION_END, this._catchTransitionEnd, this);
} }
this._addLayers(this.options.layers);
}, },
@ -854,17 +855,21 @@ export var Map = Evented.extend({
}, },
// @method getMinZoom(): Number // @method getMinZoom(): Number
// Returns the minimum zoom level of the map (if set in the `minZoom` option of the map or of any layers), or `0` by default. // Returns the minimum zoom level of the map (if set in the `minZoom` option of the map or of any layers), or the CRS's minimum zoom level by default.
getMinZoom: function () { getMinZoom: function () {
return this.options.minZoom === undefined ? this._layersMinZoom || 0 : this.options.minZoom; return Math.max(
this.options.minZoom,
this._layersMinZoom,
this.options.crs.minZoom);
}, },
// @method getMaxZoom(): Number // @method getMaxZoom(): Number
// Returns the maximum zoom level of the map (if set in the `maxZoom` option of the map or of any layers). // Returns the maximum zoom level of the map (if set in the `maxZoom` option of the map or of any layers), or the CRS's maximum zoom level by default.
getMaxZoom: function () { getMaxZoom: function () {
return this.options.maxZoom === undefined ? return Math.min(
(this._layersMaxZoom === undefined ? Infinity : this._layersMaxZoom) : this.options.maxZoom,
this.options.maxZoom; this._layersMaxZoom,
this.options.crs.maxZoom);
}, },
// @method getBoundsZoom(bounds: LatLngBounds, inside?: Boolean, padding?: Point): Number // @method getBoundsZoom(bounds: LatLngBounds, inside?: Boolean, padding?: Point): Number