如何删除 places 图层中 geojson 源下要素集合中的特定要素?

How to remove specific feature in feature collection under geojson source in a places layer?

我知道了:

map.addLayer({
  "id": "places",
  "type": "symbol",
  "source": {
    "type": "geojson",
    "data": {
      "type": "FeatureCollection",
      "features": features
    }
  },
  "layout": {
    "icon-image": "{icon}-15",
    "text-field": "{title}",
    "text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
    "text-offset": [0, 0.6],
    "text-anchor": "top"
  }
});

其中 features 是这样的对象数组:

{
  "id": SOME_ID,
  "type": "Feature",
  "properties": {
    "title": SOME_TITLE,
    "icon": "monument"
  },
  "geometry": {
    "type": "Point",
    "coordinates": SOME_COORDINATES
  }
}

我想删除这个特定的功能,不是整个图层,怎么办?

我尝试为每个特征创建一个具有预定义 ID 的指定层,但是 当试图使用 map.removeLayer(SOME_ID) 删除它时,这告诉我 图层 ID 不存在。

如何从 mapbox 中的要素集合中删除特定地理json 要素,而不删除整个 层,只有 json 数据?

如果您想在地图上隐藏地图项,则不必将其从源中删除。您可以使用 filter 过滤此功能,这样它就不会在地图上呈现。

考虑示例:

    map.addLayer({
      id: 'points',
      type: 'circle',
      source: {
        type: 'geojson',
        data: {
          type: 'FeatureCollection',
          features: [
            {
              type: 'Feature',
              id: 1,
              properties: {},
              geometry: {
                type: 'Point',
                coordinates: [127.61718749999999, 66.51326044311185]
              }
            },
            {
              type: 'Feature',
              id: 2,
              properties: {},
              geometry: {
                type: 'Point',
                coordinates: [79.1015625, 72.50172235139388]
              }
            },
            {
              type: 'Feature',
              id: 3,
              properties: {},
              geometry: {
                type: 'Point',
                coordinates: [61.17187499999999, 31.952162238024975]
              }
            }
          ]
        }
      },
      filter: ['!=', '$id', 3]
    });

这将呈现除 id=3 的功能之外的所有功能。

您也可以使用 map.setFilter 方法设置此类过滤器。

map.setFilter('my-layer', ['==', 'name', 'USA']);

编辑:

如果您真的想从源中删除特定功能,您可以从 features 数组中过滤此功能并使用 setData:

更新源
map.getSource('places').setData({
  "type": "FeatureCollection",
  features
});