我正在尝试根据一组条件过滤 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/