Skip to content

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.

Click a cluster to zoom in; click a point for its magnitude

Edit this map in the playground

create-and-style-clusters.yaml
# 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: map
id: create-and-style-clusters
config:
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: 50
layers:
- 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: mag
  • Clustering is source configuration: cluster: true groups points closer than clusterRadius pixels, up to zoom clusterMaxZoom. The source sits under sources: with a name, so three layers share one fetch.
  • MapLibre gives each cluster two properties: point_count (how many points it holds) and point_count_abbreviated (the same, shortened to 1.2k). The step expressions read point_count for size and color; the label shows the abbreviated one.
  • filter: ["has", "point_count"] draws clusters only; ["!", ["has", "point_count"]] draws the single points.
  • text-font must name a font the basemap’s glyphs include, or the counts don’t draw. Noto Sans Regular is in the demo tiles and OpenFreeMap styles.
  • click.zoomToFeature zooms 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.