Querying features in mapbox which are not visible

Viewed 20

I have a mapbox map with lots of geometries (100 000+ features).

I'd like to draw a rectangle and select features, which belong to the bounds drawn out.
Mapbox provides such feature via queryRenderedFeatures()

Something like this example:
https://docs.mapbox.com/mapbox-gl-js/example/using-box-queryrenderedfeatures/

The selection is made by this code snippet:

const features = map.queryRenderedFeatures(bbox, {
     layers: ['myFirstLayer', 'mySecondLayer', 'myThirdLayer']
});

This one selects only features, that are currently rendered. In my scenario all my layers have a minzoom layout property set, to display only at certain zoom.

But after drawing the rectangle, I'd like to select all features which are on my map - either visible or invisible.

Does mabpox provide this functionality, or do I have to do that on my own?

1 Answers

If you want to query all features, not only features rendered on the map, you could use:

map.querySourceFeatures(
    'sourceID',
    { sourceLayer: 'LayerID' },
);

You can add additional filter parameters, as documented here. But it is not possible to pass a bbox as an argument to filter features within that bbox. But you could use points-within-poygon by turf.

Related