如何正确使用Mapbox中的setFilter

问题描述 投票:0回答:1

我正在尝试从 geojson 文件过滤添加到地图中的图层。每次我单击按钮来过滤地图时,它都会隐藏整个图层,而不是该图层上的单个元素。有人可以告诉我哪里出了问题/不理解 .setFilter()

我尝试了多种使用过滤器的方法,例如map.setFilter('allBuildings', ['==', 'properties.name', 'buildingone']);

但是,它仍然隐藏了整个图层。如果有人能将我推向正确的方向,我将不胜感激。谢谢

这里我将源和图层添加到地图中,一切正常。

map.addSource('buildings', {type: 'geojson', data: '/buildings.geojson'});

map.addLayer({
  "id": "allBuildings",
  "type": "symbol",
  "source": "buildings",
  "layout": {
    "icon-image": "marker-building",
    "icon-size": 0.25
  }
});

然后我尝试使用

过滤图层
map.setFilter('allBuildings', ['>', 'properties.height', '30']);

此时,它会隐藏图层,而不仅仅是高度大于 30 的对象。

这是我的 geojson 文件的一部分。

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "buildingone",
        "height": "52",
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          114.1454573,
          22.2760386
        ]
      }
    }
  ]
}
javascript vue.js mapbox
1个回答
0
投票

总结您应该使用此命令的评论的响应

map.setFilter('allBuildings', ['>', ['to-number', ['get', 'height']], 30])

使用

get
表达式检索属性值。然后将检索到的字符串转换为数字,最后比较。它也适用于 MapLibre。

© www.soinside.com 2019 - 2024. All rights reserved.