Mapbox-gl-js: How to pass map's geographical bounds to queryRenderedFeatures ?

Created on 27 Nov 2017  路  1Comment  路  Source: mapbox/mapbox-gl-js

Objective : Need to fetch particular feature from the layer which might not be there in the viewport.
To achieve this I tried giving geographical bounds to the function but its not returning any feature.

This is the code-snippet at current

mapBox.queryRenderedFeatures([
[initialMapBounds['sw_lng'] , initialMapBounds['sw_lat']]
,[initialMapBounds['ne_lng'] , initialMapBounds['ne_lat']]
],
{
layers: ['stateAreas']
});
I have assured that feature does exists within the provided bounding box but somehow this is not working. Can you provide any relevant example to achieve this ?

Most helpful comment

You can ask "how do I" questions like this at https://stackoverflow.com/questions/tagged/mapbox-gl-js

As documented at https://www.mapbox.com/mapbox-gl-js/api/#map#queryrenderedfeatures the geometry parameter to queryRenderedFeatures accepts a "A Point or an array of two numbers representing x and y screen coordinates in pixels." not a geographic bounds in lat long. But queryRenderedFeatures is only designed to query within the current viewport, not outside since the tiles with the vector data might not be loaded.

You can't reliably get features client side outside the current viewport, even when using querySourceFeatures. Instead you could use the "Retrieve features from vector tiles" API https://www.mapbox.com/api-documentation/#retrieve-features-from-vector-tiles

>All comments

You can ask "how do I" questions like this at https://stackoverflow.com/questions/tagged/mapbox-gl-js

As documented at https://www.mapbox.com/mapbox-gl-js/api/#map#queryrenderedfeatures the geometry parameter to queryRenderedFeatures accepts a "A Point or an array of two numbers representing x and y screen coordinates in pixels." not a geographic bounds in lat long. But queryRenderedFeatures is only designed to query within the current viewport, not outside since the tiles with the vector data might not be loaded.

You can't reliably get features client side outside the current viewport, even when using querySourceFeatures. Instead you could use the "Retrieve features from vector tiles" API https://www.mapbox.com/api-documentation/#retrieve-features-from-vector-tiles

Was this page helpful?
0 / 5 - 0 ratings