首页 > 解决方案 > 如何更新deckgl-mapbox MapboxLayer的可见道具

问题描述

我有 10-15 个不同的 layerId 图层,并从 deck.gl/mapbox 子模块 api 引用创建MapboxLayer并添加到 mapbox 地图实例中。

尝试通过调用layerVisibility方法从 UI 传递 layerId 和 propertyValue 作为 true/false 复选框来设置图层的可见性,但它不起作用。

DeckglLayer 类:

declare let deck;
export class DeckglLayer {
    constructor() {

    }
    createDeckglMapboxLayer(layerId, data) {
        const { MapboxLayer, HexagonLayer } = deck;
        const options = {
            radius: 1000,
            coverage: 1,
            upperPercentile: 100
        };
    ...
    ...
        const hexagonLayer = new MapboxLayer({
            type: HexagonLayer,
            id: layerId,
            data,
            colorRange: COLOR_RANGE,
            elevationRange: [0, 20000],
            elevationScale: 4,
            extruded: true,
            getPosition: d => d.COORDINATES,
            getElevationValue: e => Math.sqrt(e[0].properties.height) * 10,
            getColorValue: this.getColorValue,
            lightSettings: LIGHT_SETTINGS,
            pickable: true,
            onHover: setTooltip,
            opacity: 1,
            visible: true,
            ...options
        });
        return hexagonLayer;
    }
}

Mapbox 实例:

 createMap(mapOptions) {
        this.mapInstance = new MapboxGl.Map(mapOptions);
    }

    addDeckglLayer(layerId, data) {
        var hexalayer = new DeckglLayer().createDeckglMapboxLayer(layerId, data);
        this.mapInstance.addLayer(hexalayer);
    }

    layerVisibility(layerId,propertyValue) {
        var ll: any = this.mapInstance.getLayer(layerId);
        //***********first way
        // ll.implementation.props.visible = propertyValue;
        //this.mapInstance.addLayer(ll);

        //*******second way
        //ll.setProps({ visible: propertyValue });
    }

注意:- 我尝试将图层布局属性可见性设置为“可见”或“无”,但在这种情况下,即使图层隐藏,工具提示也会出现。

您能否建议我最好的方法,如何为 MapboxLayer 的 hexagonLayer 类型设置图层可见属性 true/false。

标签: mapbox-gldeck.gl

解决方案


试试这个,它对我有用。

let refreshedLayers = [];
let currLayers = map.__deck.layerManager.getLayers();
let layer = currLayers[0];

// make it visible
// newLayer = layer.clone({ visible: true });
// make it unvisible
newLayer = layer.clone({ visible: false });
refreshedLayers.push(newLayer);

map.__deck.setProps({ layers: refreshedLayers })

推荐阅读