Skip to content

Dynamic styling of cluster depending on a feature in it #12978

@dmcbrn

Description

@dmcbrn

2.15.0:

Question

I'd like to change the coloring of a cluster when it contains a specific Property of a Feature.

I use the method below to change the style of a single marker.

map.setLayoutProperty(
        'location-point',
        'icon-image',
        [
            'case',
            ['==', ['get', 'id'], mapLocationActive],
            'marker',
            'marker-alt'
        ]
    );

This works fine.

I would like to do the similar thing (distinguish active from other clusters) when my location is inside a cluster

map.addLayer({
            id: 'clusters',
            type: 'circle',
            source: 'locations',
            filter: ['has', 'point_count'],
            paint: {
                'circle-color': 'white',
                'circle-radius': 30,
            }
        });

something like this but I don't know how to do the condition part

map.setPaintProperty( //EDITED AS THIS WAS setLayoutProperty( PREVIOUSLY
        'clusters',
        'circle-color',
        [
            'case',
            ['in', ['getClusterLeaves', 'id'], mapLocationActive], //this is wrong
            'red',
            'white'
        ],
    );

Links to related documentation

https://docs.mapbox.com/mapbox-gl-js/example/cluster/
https://docs.mapbox.com/style-spec/reference/expressions/#in
https://docs.mapbox.com/mapbox-gl-js/api/sources/#geojsonsource#getclusterleaves

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions