Skip to content

轨迹热力图 ​

轨迹热力图示例。 热力图 GeoJSON 矢量 trajectory WebGL

本示例展示使用 ol/layer/Heatmap 图层渲染线性几何图形。 数据是哥德堡(Gothenburg)周边的 AIS 海上交通数据,已转换为 GeoJSON,取自 此示例。

main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import HeatmapLayer from 'ol/layer/Heatmap.js';
import TileLayer from 'ol/layer/Tile.js';
import {fromLonLat} from 'ol/proj.js';
import StadiaMaps from 'ol/source/StadiaMaps.js';
import VectorSource from 'ol/source/Vector.js';

const blur = document.getElementById('blur');
const radius = document.getElementById('radius');

const heatmap = new HeatmapLayer({
  source: new VectorSource({
    url: 'data/geojson/ship-trajectories.json',
    format: new GeoJSON(),
    attributions: 'Danish Maritime Authority',
  }),
  blur: ['/', ['var', 'blur'], 2],
  radius: ['/', ['var', 'radius'], 2],
  variables: {
    blur: parseInt(blur.value, 10),
    radius: parseInt(radius.value, 10),
    shipType: 'All',
  },
  filter: [
    'any',
    ['==', ['var', 'shipType'], 'All'],
    ['==', ['var', 'shipType'], ['get', 'ShipType']],
  ],
  weight: () => 0.1,
});

const raster = new TileLayer({
  source: new StadiaMaps({
    layer: 'alidade_smooth_dark',
  }),
});

const map = new Map({
  layers: [raster, heatmap /*vector*/],
  target: 'map',
  view: new View({
    center: fromLonLat([11.86, 57.67]),
    zoom: 12,
  }),
});

blur.addEventListener('input', function () {
  heatmap.updateStyleVariables({blur: parseInt(blur.value, 10)});
});

radius.addEventListener('input', function () {
  heatmap.updateStyleVariables({radius: parseInt(radius.value, 10)});
});

const shipTypeSelect = document.getElementById('shiptype-filter');
shipTypeSelect.addEventListener('input', function () {
  heatmap.updateStyleVariables({shipType: shipTypeSelect.value});
  map.render();
});
index.html
html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>轨迹热力图</title>
    <link rel="stylesheet" href="node_modules/ol/ol.css">
    <style>
      .map {
        width: 100%;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <form>
      <label for="radius">半径大小</label>
      <input id="radius" type="range" min="1" max="50" step="1" value="5"/>
      <label for="blur">模糊大小</label>
      <input id="blur" type="range" min="1" max="50" step="1" value="15"/>
      <label for="shiptype-filter">按船舶类型筛选:</label>
      <select id="shiptype-filter">
          <option>全部</option>
          <option>客船</option>
          <option>高速船(HSC)</option>
          <option>油轮</option>
          <option>货船</option>
          <option>帆船</option>
          <option>其他</option>
          <option>拖船</option>
          <option>搜救船(SAR)</option>
          <option>游艇</option>
          <option>挖泥船</option>
          <option>执法船</option>
          <option>引航船</option>
          <option>渔船</option>
          <option>潜水船</option>
          <option>备用 2</option>
          <option>未定义</option>
      </select>
    </form>

    <script type="module" src="main.js"></script>
  </body>
</html>
package.json
json
{
  "name": "heatmap-trajectories",
  "dependencies": {
    "ol": "10.11.0"
  },
  "devDependencies": {
    "vite": "^8.1.5"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}