Realtime Stream
Runnable MapLibre GL JS example: Realtime Stream. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:实时数据流
核心逻辑:定时把新点位并入数组,setData 全量替换,只保留最近 40 条
依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式
-->
<!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 b{color:#16181d;font-weight:600}
.btn{border:1px solid #cdd3db;background:#fff;border-radius:4px;padding:3px 9px;
font:12px/1.5 inherit;color:#16181d;cursor:pointer}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<button class="btn" id="toggle">暂停</button>
<span>缓冲 <b id="n">0</b> 条</span>
<span>最新 <b id="last">—</b></span>
<span>每 0.9 秒推入一条</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 MAX = 40;
var points = [];
var seq = 1;
var map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [120.2, 30.25],
zoom: 8.6,
attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
function collection() {
return {
type: 'FeatureCollection',
features: points.map(function (p) {
return {
type: 'Feature',
properties: { seq: p.seq, value: p.value },
geometry: { type: 'Point', coordinates: [p.lng, p.lat] }
};
})
};
}
map.on('load', function () {
map.addSource('live', { type: 'geojson', data: collection() });
map.addLayer({
id: 'live-halo',
type: 'circle',
source: 'live',
paint: {
'circle-radius': 13,
'circle-color': '#e8590c',
'circle-opacity': 0.16
}
});
map.addLayer({
id: 'live',
type: 'circle',
source: 'live',
paint: {
'circle-radius': ['step', ['get', 'value'], 4, 40, 6, 70, 8],
'circle-color': [
'step', ['get', 'value'],
'#4a90c9', 40, '#e8590c', 70, '#b42318'
],
'circle-stroke-color': '#ffffff',
'circle-stroke-width': 1.2
}
});
});
function push() {
points.push({
lng: 120.05 + Math.random() * 0.32,
lat: 30.12 + Math.random() * 0.28,
value: Math.round(15 + Math.random() * 85),
seq: seq++
});
if (points.length > MAX) points.shift();
var src = map.getSource('live');
if (src) src.setData(collection());
var last = points[points.length - 1];
document.getElementById('n').textContent = points.length;
document.getElementById('last').textContent =
last.value + ' · #' + last.seq;
}
var timer = setInterval(push, 900);
push();
document.getElementById('toggle').addEventListener('click', function () {
if (timer) {
clearInterval(timer);
timer = null;
this.textContent = '继续';
} else {
timer = setInterval(push, 900);
this.textContent = '暂停';
}
});
map.on('error', function (e) {
if (e.error) console.warn('地图错误:' + e.error.message);
});
})();
</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.