setFilter(
layerId:string,filter?:FilterSpecification,options?:StyleSetterOptions):this
Defined in: ui/map.ts:3673
Sets the filter for the specified style layer.
Filters control which features a style layer renders from its source.
Any feature for which the filter expression evaluates to true will be
rendered on the map. Those that are false will be hidden.
Use setFilter to show a subset of your source data.
To clear the filter, pass null or undefined as the second parameter.
Parameters
| Parameter | Type | Description |
|---|---|---|
layerId |
string |
The ID of the layer to which the filter will be applied. |
filter? |
FilterSpecification |
The filter, conforming to the MapLibre Style Specification's filter definition. If null or undefined is provided, the function removes any existing filter from the layer. |
options? |
StyleSetterOptions |
Options object. |
Returns
this
Examples
Display only features with the 'name' property 'USA'
map.setFilter('my-layer', ['==', ['get', 'name'], 'USA']);
Display only features with five or more 'available-spots'
map.setFilter('bike-docks', ['>=', ['get', 'available-spots'], 5]);
Remove the filter for the 'bike-docks' style layer
map.setFilter('bike-docks', null);
See
Create a timeline animation