This function sets a filter on a map layer, working with both regular map objects and proxy objects.
Clustered layers
A layer created via the cluster_options shortcut in add_circle_layer()
or add_symbol_layer() is actually three layers over one source
("id", "id-clusters", "id-cluster-count"). set_filter() targets
exactly one of them, so:
Calling
set_filter("id", ...)applies only to the unclustered sub-layer. Cluster circles still show the pre-filter counts.Cluster points are synthetic (their only properties are
point_count,cluster_id, etc.), so a filter that reads a feature property like"year"cannot be meaningfully applied to the-clusterslayer — it would evaluate toFALSEand hide all clusters.
For the common "filter my data" case on a clustered map, use
set_source() instead. It replaces the source's data and
Mapbox/MapLibre re-cluster automatically:
set_filter() is still the right tool for cluster-aware filters
that read cluster-point properties, e.g. hiding clusters below a
count threshold:
