Atlaslog
Atlaslog / Examples / MapLibre GL JS / Live Update
MapLibre GL JS · Runnable Example

Live Update

Runnable MapLibre GL JS example: Live Update. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

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

Source

demos/maplibre-live-update.html
<!--
  示例:实时更新数据
  核心逻辑:定时把新位置写入两个 GeoJSON 源,轨迹线增长、车辆点前移
  依赖: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:10px;align-items:center;padding:8px 12px;
       background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar button{font:inherit;font-size:12px;padding:4px 10px;border-radius:4px;cursor:pointer;
       border:1px solid #ccd1d9;background:#fff;color:#16181d}
  .bar button:hover{background:#eef0f3}
  .bar b{color:#16181d;font-weight:600}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <button id="toggle">暂停</button>
  <span>已采 <b id="n">0</b> 个点</span>
  <span>最后更新 <b id="t">—</b></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 waypoints = [
    [116.391, 39.907], [117.200, 39.133], [117.000, 36.651],
    [118.797, 32.060], [121.474, 31.230]
  ];

  var path = [];
  for (var i = 0; i < waypoints.length - 1; i++) {
    var a = waypoints[i], b = waypoints[i + 1];
    for (var k = 0; k < 6; k++) {
      path.push([
        a[0] + (b[0] - a[0]) * k / 6,
        a[1] + (b[1] - a[1]) * k / 6
      ]);
    }
  }
  path.push(waypoints[waypoints.length - 1]);

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

  var timer = null;
  var index = 1;

  map.on('load', function () {
    map.addSource('trail', {
      type: 'geojson',
      data: { type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [path[0]] } }
    });

    map.addSource('bus', {
      type: 'geojson',
      data: { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: path[0] } }
    });

    map.addLayer({
      id: 'trail-line',
      type: 'line',
      source: 'trail',
      layout: { 'line-cap': 'round', 'line-join': 'round' },
      paint: { 'line-color': '#1f5fd0', 'line-width': 3 }
    });

    map.addLayer({
      id: 'bus-dot',
      type: 'circle',
      source: 'bus',
      paint: {
        'circle-radius': 6,
        'circle-color': '#c2410c',
        'circle-stroke-color': '#fff',
        'circle-stroke-width': 2
      }
    });

    timer = setInterval(tick, 700);
  });

  function tick() {
    if (index >= path.length) {
      index = 1;
    }

    var visited = path.slice(0, index + 1);

    map.getSource('trail').setData({
      type: 'Feature',
      properties: {},
      geometry: { type: 'LineString', coordinates: visited }
    });

    map.getSource('bus').setData({
      type: 'Feature',
      properties: {},
      geometry: { type: 'Point', coordinates: path[index] }
    });

    document.getElementById('n').textContent = visited.length;
    document.getElementById('t').textContent =
      new Date().toLocaleTimeString('zh-CN', { hour12: false });

    index++;
  }

  document.getElementById('toggle').addEventListener('click', function () {
    if (timer) {
      clearInterval(timer);
      timer = null;
      this.textContent = '继续';
    } else {
      timer = setInterval(tick, 700);
      this.textContent = '暂停';
    }
  });

  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 · EXAMPLEMap Export MapLibre GL JS · EXAMPLEMarker MapLibre GL JS · EXAMPLEMax Bounds