Atlaslog
Atlaslog / Examples / OpenLayers / Heatmap
OpenLayers · Runnable Example

Heatmap

A canvas heatmap over a street basemap, with radius, blur and weight controls.

Open standalone → Chinese notes All OpenLayers examples
/demos/openlayers-heatmap.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-heatmap.html
<!--
  示例:热力图
  要点:ol.layer.Heatmap 渲染密集点,radius 与 blur 可在运行时调整
  依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!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/ol@10.3.1/ol.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}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:18px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar label{display:flex;gap:6px;align-items:center}
  .bar input[type=range]{width:110px}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <label>半径 <input type="range" id="radius" min="3" max="20" value="9"><b id="radiusVal">9</b></label>
  <label>模糊 <input type="range" id="blur" min="5" max="30" value="15"><b id="blurVal">15</b></label>
  <span>共 1200 个采样点</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
  'use strict';

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

  var fromLonLat = ol.proj.fromLonLat;

  /* 四个聚集中心,模拟人流在城区的分布 */
  var centers = [
    [116.391, 39.907, 0.9],
    [116.460, 39.915, 0.8],
    [116.330, 39.940, 0.6],
    [116.420, 39.870, 0.5]
  ];

  function randNormal() {
    /* 两个均匀分布相加近似正态,让点自然聚成团 */
    return (Math.random() + Math.random() + Math.random() - 1.5) / 1.5;
  }

  var features = [];
  var total = 1200;
  var i;

  for (i = 0; i < total; i++) {
    var c = centers[i % centers.length];
    var lon = c[0] + randNormal() * 0.035 / c[2];
    var lat = c[1] + randNormal() * 0.028 / c[2];

    features.push(new ol.Feature({
      geometry: new ol.geom.Point(fromLonLat([lon, lat])),
      weight: 0.4 + Math.random() * 0.6
    }));
  }

  var heatLayer = new ol.layer.Heatmap({
    source: new ol.source.Vector({ features: features }),
    blur: 15,
    radius: 9,
    weight: function (feature) {
      return feature.get('weight');
    },
    gradient: ['#2b3a8f', '#1f8fc4', '#37b36b', '#f2d24b', '#e2672a', '#b8241c']
  });

  new ol.Map({
    target: 'map',
    layers: [
      new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) }),
      heatLayer
    ],
    view: new ol.View({
      center: fromLonLat([116.400, 39.915]),
      zoom: 11
    })
  });

  var radiusInput = document.getElementById('radius');
  var blurInput = document.getElementById('blur');

  radiusInput.addEventListener('input', function () {
    heatLayer.setRadius(Number(this.value));
    document.getElementById('radiusVal').textContent = this.value;
  });

  blurInput.addEventListener('input', function () {
    heatLayer.setBlur(Number(this.value));
    document.getElementById('blurVal').textContent = this.value;
  });
})();
</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 OpenLayers examples

OpenLayers · EXAMPLEHeatmap Earthquakes OpenLayers · EXAMPLEHeatmap Gradient OpenLayers · EXAMPLEHeatmap Weight