Mapbox 更改源属性

Joh*_*sma 3 javascript mapbox mapbox-gl mapbox-gl-js

我想用mapbox制作一个簇层,是的,我做到了:D。但我希望能够将其关闭

所以我正在制作一个来源

        const source = {
            type: "geojson",
            data: {
                "type": "FeatureCollection",
                "features": []
            },
            cluster: true,
            clusterRadius: 10
        }
        this.map.addSource(id, source);
Run Code Online (Sandbox Code Playgroud)

现在我可以设置数据:

        this.map.getSource(this.id).setData({
            "type": "FeatureCollection",
            "features": this.createInnerCircles()
        })
Run Code Online (Sandbox Code Playgroud)

之后,我将设置簇和圆的图层。现在集群正在工作

但现在:我如何cluster: true从我的源设置为 false。好吧,也许我可以扔掉源代码并制作一个新的源代码,但这很丑陋。

所以丑陋的方式是

        const SOURCE = this.map.getSource(id);
        SOURCE._options.cluster = false;
        this.map.removeSource(id);
        this.map.addSource(id, SOURCE._options);
Run Code Online (Sandbox Code Playgroud)

哎呀,那不起作用,现在我得到:

Source "test" cannot be removed while layer "test-outer" is using it.
Run Code Online (Sandbox Code Playgroud)

更新

我能够启用/禁用集群感谢@Steve Bennett

Source "test" cannot be removed while layer "test-outer" is using it.
Run Code Online (Sandbox Code Playgroud)

Ste*_*ett 6

更改没有特定方法的属性的一般方法是:

const style = map.getStyle()

style.sources.X.Y = Z;

map.setStyle(style)
Run Code Online (Sandbox Code Playgroud)

Mapbox GL JS 将执行比较,然后进行您需要的更改。