Mapbox setFilter 方法未按预期工作

发布于 2025-01-10 16:42:23 字数 1928 浏览 1 评论 0原文

我目前正在将一个用Vue2制作的应用程序移植到基于Vue3的Quasar框架。有一张用 Mapbox GL 1.6.1 制作的地图,我必须使用当前版本 3.6.5 进行移植。这让人非常头疼,因为我不仅是一个相当新的 Mapbox 用户,而且自 1.6.1 版本以来也发生了一些变化,更准确地说,我的问题是过滤器。

该应用程序涉及发电厂检查。检测到的异常情况会绘制在地图上,并可以根据某些标准进行显示,例如它们的优先级(低、中或高)。构建图层时,geojson 是使用来自后端的数据动态创建的,因此除了“类型”和“几何”之外,还设置了“属性”,其中包含可用于过滤地图上显示内容的数据。

正如您所期望的,图层本身按几何类型过滤源,当显示在控制台上时,我在两个 Mapbox 版本之间检测到了第一个差异。在 1.6.1 上该层有:

filter: Array(3)
  0: "=="
  1: "$type"
  2: "Polygon"
  length: 3
  ...
paint: { ... }
source: "source-name"
type: "fill"

而在 3.6.5 上有:

filter: Proxy
  [[Handler]]: Object
  [[Target]]: Array(3)
    0: "=="
    1: "$type"
    2: "Polygon"
    ...

在旧版本中有一个数组,在新版本中有一个对象,这是不一样的。我想这本身应该不是问题,因为是 Mapbox 处理数据格式,但是当我尝试向地图添加过滤器时,麻烦就来了。该应用程序的两个版本都是这样做的:

const layer = layer-name
const filter = ['==', 'severity', 3]

this.map.setFilter(layer, filter)  // old app, options api syntax
map.value.setFilter(layer, filter) // new app, composition api syntax

麻烦来了。在旧应用程序中,执行该方法后,图层的过滤器属性不会更改,正如预期的那样,它会从源中过滤多边形要素,并且仅重新调整属性“严重性”== 3 的多边形要素。但是,当将图层的内容记录在新应用程序出了问题:

filter: Proxy
  [[Handler]]: Object
  [[Target]]: Array(3)
    0: "=="
    1: "severity"
    2: "3"
    ...

显然它隐藏了所有多边形。换句话说,在 Mapbox 1.6.1 版本中,map.setFilter 方法会过滤数据源中“properties”属性的内容,但在 3.6.5 中,它会更改图层的“filter”属性。我查看了mapbox文档并在 https://docs.mapbox.com/mapbox-gl-js/example/filter-features-within-map-view/

map.on('movestart', () => {
  // reset features filter as the map starts moving
  map.setFilter('airport', ['has', 'abbrev']);
});

并尝试了类似的操作:

map.value.setFilter(layer-name, ['==', ['get', 'severity'], 3)

但它不起作用。也查看过论坛,但没有发现与我类似的问题。我确信有些东西我不明白,有人可以告诉我应该如何进行过滤吗?谢谢。

I am currently porting an app made with Vue2 to Quasar framework, which is based on Vue3. There is a map made with Mapbox GL 1.6.1 which I have to port using current version which is 3.6.5. It is giving quite a headache as, not only I am a rather new mapbox user, but also a few things have changed since 1.6.1 version, to be more precise my problem is with filters.

The app deals with power plant inspections. Anomalies detected are painted on a map and can be displayed according some criteria, their priority for instance (low, middle or high). When building the layer a geojson is made dinamically with data from the backend, so besides 'type' and 'geometry' also 'properties' is set, containing data which can be used to filter what is shown on the map.

The layer itself as you would expect filters the source by geometry type, when displayed on console there is the first difference I have detected between both mapbox versions. On 1.6.1 the layer has:

filter: Array(3)
  0: "=="
  1: "$type"
  2: "Polygon"
  length: 3
  ...
paint: { ... }
source: "source-name"
type: "fill"

while on 3.6.5 it has:

filter: Proxy
  [[Handler]]: Object
  [[Target]]: Array(3)
    0: "=="
    1: "$type"
    2: "Polygon"
    ...

In the older version there is an array, in the new one there is an object, which is not the same. I guess this in itself should not be a problem, as it is mapbox who deals with the data format, but the trouble comes when I try to add a filter to the map. Both versions of the app do it like this:

const layer = layer-name
const filter = ['==', 'severity', 3]

this.map.setFilter(layer, filter)  // old app, options api syntax
map.value.setFilter(layer, filter) // new app, composition api syntax

Here comes trouble. In the old app after executing the method the filter property of the layer is unchanged, as expected it filters polygon features from the source and only rezzes those with the property 'severity' == 3. But when logging the content of the layer in the new app something is wrong:

filter: Proxy
  [[Handler]]: Object
  [[Target]]: Array(3)
    0: "=="
    1: "severity"
    2: "3"
    ...

and obviously it hides all polygons. Said in another way, in Mapbox version 1.6.1 the map.setFilter method filters the content of the 'properties' property in the data source, but in 3.6.5 it changes the 'filter' property of the layer instead. I have looked in the mapbox docs and found something on https://docs.mapbox.com/mapbox-gl-js/example/filter-features-within-map-view/ :

map.on('movestart', () => {
  // reset features filter as the map starts moving
  map.setFilter('airport', ['has', 'abbrev']);
});

and tried something like this:

map.value.setFilter(layer-name, ['==', ['get', 'severity'], 3)

but it does not work. Also have looked on forums but have not found a problem similar to mine. I am sure there is something I don't get, could anyone please tell how the filtering should be done? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文