Mapbox GL合并过滤器问题 [英] Mapbox GL Combine Filters Issue

查看:104
本文介绍了Mapbox GL合并过滤器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mapbox GL组合过滤器出现问题.过滤器可以单独工作,但结合使用会产生错误.自治市镇过滤器使用一个可以动态更改的值数组,但我只是在代码中放了一个示例,说明它可能是什么样子.每当我尝试合并过滤器时,都会收到错误消息:预期[==,!=,>,> =,<,< =,],找到匹配""问题似乎在于,将使用匹配"表达的过滤器与使用"=="运算符的过滤器组合在一起.有谁知道如何解决这一问题.

I am having a problem with Mapbox GL combining filters. The filters work individually but when in combination produce an error. The borough filters uses an array of values that could be change dynamically but I just put an example in the code of what it might look like. Whenever I try and combine the filters I get the error: "expected one of [==, !=, >, >=, <, <=, in, !in, all, any, none, has, !has], "match" found" The problem seems to be combining a filter that uses the 'match' express with a filter using the '==' operator. Does anyone know how to fix this.

var borough_val = ["BX", "BK", "MN"];

var zipCodeFilter = ["==", 'ZipCode', Number(zipcode_val)];
var boroughFilter = ['match', ['get', 'Borough'], borough_val, true, false];

var combinedFilter = ["all", zipCodeFilter, boroughFilter];
map.setFilter('parcels_fill', combinedFilter);

推荐答案

这是我陷入过几次的陷阱.有旧语法"和新语法".

This is a trap that I have fallen into several times. There is "old syntax" and "new syntax".

旧语法: ['==','ZipCode','90210']

新语法: ['==',['get','ZipCode'],'90210']

在一定程度上,您已经注意到,旧语法仍然受支持.但是,如果您尝试结合使用旧语法和新语法,则Mapbox-GL-JS会假设整个表达式都是新语法,并且无法解析.

To a certain extent, old syntax is still supported, as you have noticed. But if you try to combine old syntax and new syntax, Mapbox-GL-JS assumes the entire expression is new syntax, and it fails to parse.

简单的解决方法是在整个过程中使用新的语法:

The simple fix is to use new syntax throughout:

var borough_val = ["BX", "BK", "MN"];

var zipCodeFilter = ["==", ['get', 'ZipCode'], Number(zipcode_val)];
var boroughFilter = ['match', ['get', 'Borough'], borough_val, true, false];

var combinedFilter = ["all", zipCodeFilter, boroughFilter];
map.setFilter('parcels_fill', combinedFilter);

(在某些情况下,由于某些原因(无法解决的歧义?需要付出太多努力?),Mapbox-GL-JS无法识别更有效的旧语法"复杂表达式.因此,通常使用起来最安全新语法到处都是.)

(There are also cases where or some reason (unresolvable ambiguities? too much effort?), Mapbox-GL-JS fails to recognise more complex expressions that were valid "old syntax". For that reason, it's generally safest to use new syntax everywhere.)

这篇关于Mapbox GL合并过滤器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆