Skip to content

Update a feature in realtime

Replicates the MapLibre example Update a feature in realtime: live USGS earthquakes, updated in place.

Circles update as the hourly feed refreshes

Edit this map in the playground

update-a-feature-in-realtime.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/update-a-feature-in-realtime/
# The USGS feed updates in place: `merge` matches each feature by its `url`
# property (the earthquake's event page, one per earthquake) and updates it,
# rather than replacing the whole collection.
type: map
id: update-a-feature-in-realtime
config:
center: [-120, 40]
zoom: 3
mapStyle: "https://demotiles.maplibre.org/style.json"
layers:
- id: quakes-live
type: circle
source:
type: geojson
url: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson"
refresh:
refreshInterval: 30000
updateStrategy: merge
updateKey: url
paint:
circle-radius:
["interpolate", ["linear"], ["get", "mag"], 1, 4, 6, 24]
circle-color: "#e63946"
circle-opacity: 0.7
circle-stroke-width: 1
circle-stroke-color: "#ffffff"
interactive:
click:
popup:
- h4:
- property: place
- p:
- str: "Magnitude: "
- property: mag
  • updateStrategy: merge updates the features whose updateKey property matches, adds new ones and keeps the rest, where upstream diffs the data by hand.
  • updateKey names a property. USGS features have no id property (their id sits outside properties), so the key here is url, the earthquake’s event page, which is unique to each one.
  • The full pattern, with what merge never removes and a vehicle feed of your own: Live data › Merge.