如何获取已过滤的Mapbox图层的结果?

时间:2017-08-19 10:08:59

标签: mapbox mapbox-gl-js

我正在尝试将旧的Mapbox.js地图更新为Mapbox GL。我正在从geojson生成地图(并使用coffescript)。

map.addSource 'my_datasource',
  'type': 'geojson'
  'data': my_geojson

map.addLayer
  'id': 'my_layer'
  'type': 'symbol'
  'source': 'my_datasource'

我根据返回value

的用户输入过滤图层
map.setFilter('my_layer', ["==", 'my_attribute', value ])

到目前为止,这么好。但现在我想缩放并重新定位地图以适应过滤符号的边界。

我以为我可以做这样的事情

bounds = new (mapboxgl.LngLatBounds)
map.queryRenderedFeatures(layers: [ 'my_layer' ]).forEach (feature) ->
  bounds.extend feature.geometry.coordinates
  return
map.fitBounds bounds

queryRenderedFeatures似乎正在返回所有(即未过滤的)符号。

经过大量阅读后,我的理解是queryRenderedFeatures应该返回在视口中可见的滤波符号(即,适合放大而不是缩小)。

这是对的吗?如果是这样,为什么我的函数会返回未经过滤的符号?感谢任何建议,以帮助我过渡到MapboxGL!

2 个答案:

答案 0 :(得分:1)

从文档中我不清楚是否应该应用图层中的过滤器,但在任何情况下,都有一个明确的filter参数可以传递,所以:

map.queryRenderedFeatures(layers: [ 'my_layer' ],  filter:["==", 'my_attribute', value ]).forEach (feature) ->
  bounds.extend feature.geometry.coordinates

但我怀疑你真的想要querySourceFeatures,因为你不想受到视口中当前内容的约束:

map.querySourceFeatures(my_source,  filter:["==", 'my_attribute', value ]).forEach (feature) ->
  bounds.extend feature.geometry.coordinates

或者,在原生ES2015中:

map.querySourceFeatures(my_source,  { filter:['==', 'my_attribute', value ]} )
   .forEach (feature => bounds.extend(feature.geometry.coordinates))

答案 1 :(得分:0)

试试这篇文章,我已经添加了代码,它可以让你使用 queryRenderedFeatures() 甚至使用 querySourceFeatures() 来拥有这些功能: https://stackoverflow.com/a/66308173/9185662

相关问题