Atlaslog
Atlaslog / Examples / MapLibre GL JS / Heatmap
MapLibre GL JS · Runnable Example

Heatmap

A density heatmap driven by a vector source and paint expressions.

Open standalone → Chinese notes All MapLibre GL JS examples
/demos/maplibre-heatmap.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-heatmap.html
<!--
  示例:热力图
  核心逻辑:随机点按权重渲染 heatmap 图层,滑块调整影响半径
  依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 免 Key 样式
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>热力图</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.css">
<style>
  html,body{height:100%;margin:0}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
  .bar{flex:0 0 auto;display:flex;gap:12px;align-items:center;padding:8px 12px;
       background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar input[type=range]{width:160px}
  .bar b{color:#16181d;font-weight:600}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <label>影响半径 <input id="r" type="range" min="10" max="60" step="1" value="30"> <b id="rv">30 px</b></label>
  <span>800 个带权重点 · 缩放 2–4 观察密度</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.js"></script>
<script>
(function () {
  'use strict';

  if (typeof maplibregl === 'undefined') {
    document.body.innerHTML =
      '<div style="padding:24px;color:#b42318">MapLibre 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  var hubs = [
    [116.39, 39.90], [121.47, 31.23], [113.26, 23.12],
    [104.06, 30.57], [108.94, 34.34], [126.64, 45.76],
    [87.61, 43.82], [102.71, 25.04]
  ];

  var features = [];
  for (var i = 0; i < 800; i++) {
    var h = hubs[i % hubs.length];
    var dx = (Math.random() + Math.random() - 1) * 1.6;
    var dy = (Math.random() + Math.random() - 1) * 1.2;
    features.push({
      type: 'Feature',
      properties: { v: Math.random() },
      geometry: { type: 'Point', coordinates: [h[0] + dx, h[1] + dy] }
    });
  }

  var points = { type: 'FeatureCollection', features: features };

  var map = new maplibregl.Map({
    container: 'map',
    style: 'https://demotiles.maplibre.org/style.json',
    center: [108, 33],
    zoom: 2.6,
    attributionControl: {
      compact: true,
      customAttribution: 'MapLibre demo tiles'
    }
  });

  map.on('load', function () {
    map.addSource('density', { type: 'geojson', data: points });

    map.addLayer({
      id: 'density-heat',
      type: 'heatmap',
      source: 'density',
      paint: {
        'heatmap-weight': ['interpolate', ['linear'], ['get', 'v'], 0, 0.2, 1, 1],
        'heatmap-intensity': 1.1,
        'heatmap-radius': 30,
        'heatmap-opacity': 0.85,
        'heatmap-color': [
          'interpolate', ['linear'], ['heatmap-density'],
          0, 'rgba(31,95,208,0)',
          0.2, '#1f5fd0',
          0.45, '#17694a',
          0.7, '#a16207',
          1, '#c2410c'
        ]
      }
    });
  });

  document.getElementById('r').addEventListener('input', function () {
    var v = parseInt(this.value, 10);
    document.getElementById('rv').textContent = v + ' px';
    if (map.getLayer('density-heat')) {
      map.setPaintProperty('density-heat', 'heatmap-radius', v);
    }
  });

  map.addControl(new maplibregl.NavigationControl(), 'top-right');
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More MapLibre GL JS examples

MapLibre GL JS · EXAMPLEHover Effect MapLibre GL JS · EXAMPLEImage Source MapLibre GL JS · EXAMPLELabel Collision