我正在编写一个基于 Mapbox GL JS 的程序。我已经将我的图层保存在 mapbox 样式中并给它们一些颜色。在客户端,单击按钮时,我正在更改该图层的颜色,
Map.setPaintProperty(layerid,'circle-color','#ff00ff')
但现在我想要一个按钮,它将图层颜色重置为原始颜色(我以 mapbox 样式给出)。
有什么想法吗?
我正在编写一个基于 Mapbox GL JS 的程序。我已经将我的图层保存在 mapbox 样式中并给它们一些颜色。在客户端,单击按钮时,我正在更改该图层的颜色,
Map.setPaintProperty(layerid,'circle-color','#ff00ff')
但现在我想要一个按钮,它将图层颜色重置为原始颜色(我以 mapbox 样式给出)。
有什么想法吗?
我认为您必须自己存储原始颜色。在设置新颜色之前:
const originalColor = map.getPaintProperty(layerid, 'circle-color');
// ...set the color on click
// on reset
map.setPaintProperty(layerid, 'circle-color', originalColore);
或者,您可以使用以下方法存储完整的原始地图样式map.getStyle()
,然后将其重置map.setStyle(originalStyle)
:https ://docs.mapbox.com/mapbox-gl-js/api/#map#getstyle