diff --git a/packages/component/src/control/layer.ts b/packages/component/src/control/layer.ts index f3762a1ea1..2fdd7d8245 100644 --- a/packages/component/src/control/layer.ts +++ b/packages/component/src/control/layer.ts @@ -175,8 +175,12 @@ export default class Layers extends Control { for (let i = inputs.length - 1; i >= 0; i--) { input = inputs[i]; layer = this.layerService.getLayer(input.layerId); - if (layer) { - input.disabled = !layer.inited || !layer.isVisible(); + + if (layer && layer.inited) { + const minZoom = layer.getMinZoom(); + const maxZoom = layer.getMaxZoom(); + + input.disabled = zoom < minZoom || zoom > maxZoom; } } } diff --git a/packages/core/src/services/layer/ILayerService.ts b/packages/core/src/services/layer/ILayerService.ts index e9fda0a895..d1c29ce8cb 100644 --- a/packages/core/src/services/layer/ILayerService.ts +++ b/packages/core/src/services/layer/ILayerService.ts @@ -141,6 +141,9 @@ export interface ILayer { isVisible(): boolean; setMaxZoom(min: number): ILayer; setMinZoom(max: number): ILayer; + getMinZoom(): number; + getMaxZoom(): number; + get(name: string): number; setBlend(type: keyof typeof BlendType): void; // animate(field: string, option: any): ILayer; render(): ILayer; diff --git a/packages/layers/src/core/BaseLayer.ts b/packages/layers/src/core/BaseLayer.ts index 68b2c08588..bae6d16c98 100644 --- a/packages/layers/src/core/BaseLayer.ts +++ b/packages/layers/src/core/BaseLayer.ts @@ -629,6 +629,22 @@ export default class BaseLayer extends EventEmitter return this; } + public getMinZoom(): number { + const { minZoom } = this.getLayerConfig(); + return minZoom as number; + } + + public getMaxZoom(): number { + const { maxZoom } = this.getLayerConfig(); + return maxZoom as number; + } + + public get(name: string) { + const cfg = this.getLayerConfig(); + // @ts-ignore + return cfg[name]; + } + public setMaxZoom(maxZoom: number): ILayer { this.updateLayerConfig({ maxZoom, diff --git a/stories/Components/components/Scale.tsx b/stories/Components/components/Scale.tsx index 0ff5e67a15..7b3b5845f9 100644 --- a/stories/Components/components/Scale.tsx +++ b/stories/Components/components/Scale.tsx @@ -1,5 +1,5 @@ // @ts-ignore -import { PointLayer, PolygonLayer, Scale, Scene } from '@antv/l7'; +import { Layers, PointLayer, PolygonLayer, Scale, Scene } from '@antv/l7'; import { Mapbox } from '@antv/l7-maps'; import * as React from 'react'; @@ -70,7 +70,16 @@ export default class ScaleComponent extends React.Component { layer.setSelect(e.featureId); }); const scaleControl = new Scale(); + const layers = { + 点图层: pointLayer, + 面图层: layer, + }; + const layerControl = new Layers({ + overlayers: layers, + }); + scene.addControl(scaleControl); + scene.addControl(layerControl); } public render() {