Create and style clusters
Replicates the MapLibre example Create and style clusters — the same USGS earthquake feed and the same three-layer treatment. Upstream also adds a click handler that zooms into a cluster; here zoomToFeature does it in YAML.
The complete document
Section titled “The complete document”# yaml-language-server: $schema=https://docs.maplibre-yaml.org/schema/latest/map.schema.json# Gallery twin of https://maplibre.org/maplibre-gl-js/docs/examples/create-and-style-clusters/# Same USGS earthquake data, same three-layer treatment; clustering is# source config, no JS.type: mapid: create-and-style-clustersconfig: center: [-103.59, 40.66] zoom: 3 mapStyle: "https://demotiles.maplibre.org/style.json"sources: earthquakes: type: geojson url: "https://maplibre.org/maplibre-gl-js/docs/assets/earthquakes.geojson" cluster: true clusterMaxZoom: 14 clusterRadius: 50layers: - id: clusters type: circle source: earthquakes filter: ["has", "point_count"] paint: circle-color: ["step", ["get", "point_count"], "#51bbd6", 100, "#f1f075", 750, "#f28cb1"] circle-radius: ["step", ["get", "point_count"], 20, 100, 30, 750, 40] interactive: hover: cursor: pointer click: zoomToFeature: maxZoom: 12 - id: cluster-count type: symbol source: earthquakes filter: ["has", "point_count"] layout: text-field: ["get", "point_count_abbreviated"] text-font: ["Noto Sans Regular"] text-size: 12 - id: unclustered-point type: circle source: earthquakes filter: ["!", ["has", "point_count"]] paint: circle-color: "#11b4da" circle-radius: 4 circle-stroke-width: 1 circle-stroke-color: "#ffffff" interactive: click: popup: - h4: - str: Earthquake - p: - str: "Magnitude: " - property: magWhat to notice
Section titled “What to notice”- Clustering is source configuration:
cluster: truegroups points closer thanclusterRadiuspixels, up to zoomclusterMaxZoom. The source sits undersources:with a name, so three layers share one fetch. - MapLibre gives each cluster two properties:
point_count(how many points it holds) andpoint_count_abbreviated(the same, shortened to1.2k). Thestepexpressions readpoint_countfor size and color; the label shows the abbreviated one. filter: ["has", "point_count"]draws clusters only;["!", ["has", "point_count"]]draws the single points.text-fontmust name a font the basemap’s glyphs include, or the counts don’t draw.Noto Sans Regularis in the demo tiles and OpenFreeMap styles.click.zoomToFeaturezooms in on a clicked cluster, here to zoom 12 (maxZoom). Upstream instead asks MapLibre for the zoom at which that cluster splits (getClusterExpansionZoom), which needs page JavaScript.