Single-City Trip Playback
Twelve random-walk trajectories across urban Beijing, 120 points each at 2-second intervals, replayed by TripsLayer on a 0–240 second timeline, with a slider and play button driving currentTime.
Business context
Customer service, safety and operations review teams use trajectory playback to check complaints and anomalous trips such as detours, long stops, route deviations or a mid-trip vehicle change. The data comes from positions reported every few seconds by an in-vehicle terminal or phone, anonymized and archived by trip ID; a trip typically has tens to hundreds of points, a city produces thousands of reviewable trips per day, and only the sampled ones need full playback.
Raw positions carry GPS drift and duplicate points, so they are usually thinned, jump-filtered and smoothed before entering deck.gl, and timestamps are converted to relative seconds; the point order inside a trip must not be shuffled. TripsLayer is chosen because it natively draws a trail from a time cursor, so a full path can be passed once and replayed statelessly without clipping coordinates frame by frame in the front end.
A frequent misinterpretation is judging vehicle speed from the animation: trail length, playback multiplier and frame rate all change the visual pace, while real speed comes from timestamp differences, not from how long a segment looks. When timestamps and currentTime use different units the whole trajectory disappears, which looks like missing data but is only a unit mismatch.
How it works
Each trajectory starts at a random point within 116.30–116.50 and 39.82–40.00, with heading perturbed by ±0.25 radians per step and a step length of about 0.0015 degrees; 120 points at 2-second spacing produce a 0–238 second series. TripsLayer takes currentTime straight from the slider, trailLength 50 keeps the most recent 50 seconds of line visible, fadeTrail attenuates the tail, and widthMinPixels 3 keeps paths legible after zooming.
currentTime and getTimestamps must share a unit: both are seconds here, and if timestamps were in milliseconds while currentTime stayed in seconds the entire scene would vanish. The ratio of trailLength to the 240-second total sets how much of a path is visible at once; 50 seconds is about 21%, and pushing past 120 seconds lights whole routes simultaneously and removes the sense of playback.
| Configuration | What it does |
|---|---|
| getTimestamps | Per-point timestamps in seconds, 2 seconds apart |
| currentTime | 0–240 second cursor shared by slider and animation |
| trailLength | 50 second trail, about 21% of the total duration |
| fadeTrail | Tail fades out to show travel direction |
Note. Keep timestamps, currentTime and trailLength in TripsLayer all in seconds; change the unit in all three places at once, since changing only one makes trajectories disappear entirely.
Source
<!--
示例:单城轨迹回放
要点:12 条 2 秒步长的随机游走轨迹由 TripsLayer 按 0–240 秒时间轴回放
依赖:deck.gl 9.0 · 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>
<style>
html,body{height:100%;margin:0;background:#0d1117}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
.bar{position:relative;z-index:5;flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
display:flex;gap:12px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:5px;cursor:pointer;
border:1px solid #303845;background:#1d2531;color:#c8d2e0}
.bar button:hover{background:#253040}
.bar button.on{background:#2c4a7a;border-color:#4f8ad6;color:#dfe9ff}
.bar .sep{width:1px;height:18px;background:#2a313c;margin:0 4px}
.bar input[type=range]{width:200px;accent-color:#4f9ef8}
#cv{flex:1 1 auto;min-height:0;width:100%;display:block}
</style>
</head>
<body>
<div class="bar">
<span>当前 <b id="t">0.0</b> s</span>
<button id="play" class="on">暂停</button>
<input id="ts" type="range" min="0" max="240" step="1" value="0">
<span class="sep"></span>
<span>轨迹 <b id="n">12</b> 条 · 每点间隔 2 s · trailLength 50</span>
</div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
'use strict';
if (typeof deck === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#ff8c74">deck.gl 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var PALETTE = [
[64, 154, 255], [86, 214, 168], [246, 186, 78],
[240, 104, 72], [176, 132, 255], [64, 206, 226]
];
var trips = [];
for (var t = 0; t < 12; t++) {
var path = [];
var ts = [];
var lng = 116.30 + Math.random() * 0.20;
var lat = 39.82 + Math.random() * 0.18;
var head = Math.random() * Math.PI * 2;
for (var s = 0; s < 120; s++) {
head += (Math.random() - 0.5) * 0.5;
lng += Math.cos(head) * 0.0015;
lat += Math.sin(head) * 0.0011;
lng = Math.max(116.27, Math.min(116.53, lng));
lat = Math.max(39.80, Math.min(40.03, lat));
path.push([lng, lat]);
ts.push(s * 2);
}
trips.push({
path: path,
timestamps: ts,
color: PALETTE[t % PALETTE.length]
});
}
var current = 0;
var playing = true;
function tripLayer() {
return new deck.TripsLayer({
id: 'trips-playback',
data: trips,
getPath: function (d) { return d.path; },
getTimestamps: function (d) { return d.timestamps; },
getColor: function (d) { return d.color; },
currentTime: current,
trailLength: 50,
fadeTrail: true,
widthMinPixels: 3,
capRounded: true,
jointRounded: true
});
}
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
initialViewState: {
longitude: 116.40,
latitude: 39.92,
zoom: 10.4,
pitch: 45,
bearing: -15,
maxPitch: 70
},
controller: true,
layers: [tripLayer()]
});
var slider = document.getElementById('ts');
var label = document.getElementById('t');
document.getElementById('play').addEventListener('click', function (e) {
playing = !playing;
e.currentTarget.classList.toggle('on', playing);
e.currentTarget.textContent = playing ? '暂停' : '播放';
});
slider.addEventListener('input', function () {
current = Number(slider.value);
label.textContent = current.toFixed(1) + ' s';
deckgl.setProps({ layers: [tripLayer()] });
});
var last = performance.now();
function frame(now) {
var dt = (now - last) / 1000;
last = now;
if (playing) {
current += dt * 12;
if (current > 240) current = 0;
slider.value = current;
label.textContent = current.toFixed(1) + ' s';
deckgl.setProps({ layers: [tripLayer()] });
}
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
window.__deck = deckgl;
})();
</script>
</body>
</html>