MapSnippets logoMapSnippets logo

Heatmap Layer with MapTiler SDK

MapTiler SDK

Visualize point density using a native heatmap layer with customizable radius and color ramp.

progress_activityLoading map...
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
  container: 'map',
  style: maptilersdk.MapStyle.STREETS,
  center: [-98, 39],
  zoom: 3
});
map.on('load', () => {
  map.addSource('quakes', {
    type: 'geojson',
    data: 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson'
  });
  map.addLayer({
    id: 'quake-heat', type: 'heatmap', source: 'quakes',
    paint: {
      'heatmap-weight': ['interpolate', ['linear'], ['get', 'mag'], 0, 0, 6, 1],
      'heatmap-radius': ['interpolate', ['linear'], ['zoom'], 0, 2, 9, 20],
      'heatmap-color': ['interpolate', ['linear'], ['heatmap-density'],
        0, 'rgba(0,0,0,0)', 0.2, '#e8d5f5', 0.4, '#ce93d8',
        0.6, '#9C27B0', 0.8, '#7b1fa2', 1, '#4a148c']
    }
  });
});

How it works

The heatmap layer type renders point density as a continuous color gradient. Each point contributes heat based on its heatmap-weight, and nearby contributions are blurred by heatmap-radius.

The heatmap-weight expression uses earthquake magnitude: stronger quakes contribute more heat. The heatmap-radius scales with zoom so the effect stays consistent across zoom levels.

The heatmap-color ramp maps the computed density (0–1) to a purple gradient. Transparent at 0 lets the basemap show through sparse areas.

Key APIs

API Description Docs
heatmap layer type Density visualization from points docs.maptiler.com/sdk-js/api/
heatmap-weight Per-point contribution to heat docs.maptiler.com/sdk-js/api/
heatmap-density Built-in expression for computed density docs.maptiler.com/sdk-js/api/

FAQ

Can I show individual points at high zoom?

Yes. Add a circle layer for the same source with minzoom and set maxzoom on the heatmap layer. The transition happens automatically.

Does the heatmap work with polygon data?

No. Heatmap layers only work with point geometries.

Common mistakes

  • Not setting heatmap-weight: without it, all points contribute equally regardless of their data values
  • Using heatmap-density outside of heatmap-color: it is only valid in the color ramp expression
  • Setting a fixed heatmap-radius: it should scale with zoom for consistent visual density
auto_awesome

Build maps using MapTiler SDK with AI

Generate code like this heatmap layer with maptiler sdk example using the MapTiler SDK skill in your IDE.