mapbox - 如何在 Mapbox GL map.setFilter 中使用 `in` 表达式

标签 mapbox mapbox-gl-js

我正在尝试根据一组条件过滤 geoJson 图层(称为“目的地”)。我的“目的地”图层有一个名为 uniqueID 的字符串类型属性,大多数时候只有一个数字 ID,但有时有多个。我想根据 ID 数组过滤该图层(使用 map.setFilter)。过滤器应该执行如下操作:如果在 ID 数组中找到“目标”层中的任何 uniqueID 值,则过滤掉该功能。

这是我的“目的地”图层的片段:

{ "type": "Feature", "properties": { "destinationLon": -74.20716879, "destinationLat": 40.69097357, "uniqueID": "2029" }, "geometry": { "type": "Point", "coordinates": [ -74.20716879, 40.69097357 ] } },
{ "type": "Feature", "properties": { "destinationLon": -74.20670807, "destinationLat": 40.69137214, "uniqueID": "984,985" }, "geometry": { "type": "Point", "coordinates": [ -74.20670807, 40.69137214 ] } },
{ "type": "Feature", "properties": { "destinationLon": -74.20651489, "destinationLat": 40.71887889, "uniqueID": "1393" }, "geometry": { "type": "Point", "coordinates": [ -74.20651489, 40.71887889 ] } }

这是我可能使用的 ID 数组的示例:[2000, 984, 1393]

我尝试使用 in 表达式(文档 here )来使用过滤器,如下所示:

let thisFilter = ["any"].concat(uniqueIDs.map(id => ["in", id, ["get", "uniqueID"]]));
map.setFilter("destinations", thisFilter);

但我不断收到此错误消息:

Error: "layers.destinations.filter[0][2]: string, number, or boolean expected, array found"

但是,文档声明如下:

["in",
    keyword: InputType (boolean, string, or number),
    input: InputType (array or string)
]: boolean

表达式中的第三个参数应该是一个数组。那么为什么我会收到该错误?

有什么想法吗?谢谢!

最佳答案

我认为您的错误与in无关。您可以通过简化表达式并将 any 从各个 in 部分中分离出来来发现这一点。

您的错误是 any 采用可变数量的参数:

['any', ['in', ...], ['in', ...]]

当您传入数组时:

['any', [['in', ...], ['in', ...]]]

我会像这样重写你的表达式:

let thisFilter = ["any", ...uniqueIDs.map(id => ["in", id, ["get", "uniqueID"]])];

关于mapbox - 如何在 Mapbox GL map.setFilter 中使用 `in` 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62477327/

相关文章:

leaflet - 将 terrain-rgb 层添加到 MaboxGL.js?

mapbox - HexagonLayer - 获取聚合数据或其值范围

javascript - MapBox GLJS - 使用 for 循环创建标记时不显示

mapbox - 样式化 MapBox GL 标记

reactjs - 使用 React 缺少 MapBox CSS

mapbox - 添加图标光环作为作为 Mapbox 图标上传的图像周围的边框

polygon - 使用mapbox gl js渲染非简化的Mapbox矢量瓦片

java - 对话框中的 MapBox 显示带有额外的灰色覆盖

javascript - 高性能 GL 三 Angular 形 Mapbox GL JS

ios - 使用 Mapbox iOS SDK 时如何设置标注 View 的背景颜色?