在不丢失图层和源的情况下清除 mapbox FeatureCollections 的干净方法?
A clean way to wipe out mapbox FeatureCollections without losing layers and sources?
我正在通过添加源和链接到它们的图层来初始化 mapbox 地图。
mapbox.addSource(MY_SOURCE, {
'type': 'geojson',
'data': {
'type': 'FeatureCollection',
'features': [],
},
});
mapbox.addLayer({
'id': MY_LAYER,
'type': 'circle',
'source': MY_SOURCE,
'paint': {
'circle-radius': 6,
'circle-color': '#d31467',
},
});
我的目标是 - 我希望一次性完成这些定义 - 如果我需要更新源代码,我只需:
mapbox.getSource(MY_SOURCE).setData(geojson);
虽然有时我需要清除所有的多边形、所有的点、所有的东西。我怎样才能在不丢失所有这些定义的情况下做到这一点?我只能看到 .removeSource
, .removeLayer
- 这告诉我我实际上需要重新创建这些定义。
有没有破坏性较小的方法?
这里有两个选择:
隐藏图层
map.setLayoutProperty(<layer-id>, 'visibility', 'none');
通过设置一个空的 geojson
从您的源中删除所有特征
map.getSource(<source-id>).setData({
type: 'FeatureCollection',
features: [] // <--- no features
});
同时删除并重新添加源和层可能不是最糟糕的选择,具体取决于您要执行此操作的频率。
我正在通过添加源和链接到它们的图层来初始化 mapbox 地图。
mapbox.addSource(MY_SOURCE, {
'type': 'geojson',
'data': {
'type': 'FeatureCollection',
'features': [],
},
});
mapbox.addLayer({
'id': MY_LAYER,
'type': 'circle',
'source': MY_SOURCE,
'paint': {
'circle-radius': 6,
'circle-color': '#d31467',
},
});
我的目标是 - 我希望一次性完成这些定义 - 如果我需要更新源代码,我只需:
mapbox.getSource(MY_SOURCE).setData(geojson);
虽然有时我需要清除所有的多边形、所有的点、所有的东西。我怎样才能在不丢失所有这些定义的情况下做到这一点?我只能看到 .removeSource
, .removeLayer
- 这告诉我我实际上需要重新创建这些定义。
有没有破坏性较小的方法?
这里有两个选择:
隐藏图层
map.setLayoutProperty(<layer-id>, 'visibility', 'none');
通过设置一个空的 geojson
从您的源中删除所有特征map.getSource(<source-id>).setData({ type: 'FeatureCollection', features: [] // <--- no features });
同时删除并重新添加源和层可能不是最糟糕的选择,具体取决于您要执行此操作的频率。