Grid
Runnable deck.gl example: Grid. 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
<!--
示例:deck.gl 网格聚合
要点:GridLayer 用米制方格统计点位,颜色聚合方式可在总量、均值、极值间切换
依赖: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:8px;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}
#cv{flex:1 1 auto;min-height:0;width:100%}
</style>
</head>
<body>
<div class="bar">
<span>点数 <b id="n">—</b></span>
<span>方格 700 m</span>
<span class="sep"></span>
<span>颜色聚合</span>
<button data-agg="SUM" class="on">总量</button>
<button data-agg="MEAN">均值</button>
<button data-agg="MAX">最大值</button>
<span class="sep"></span>
<span>拖拽旋转 · 滚轮缩放</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 points = [];
for (var i = 0; i < 60000; i++) {
var t = Math.random();
var lng = 121.30 + t * 0.28 + (Math.random() - 0.5) * 0.03;
var lat = 31.18 + Math.sin(t * Math.PI) * 0.05 + (Math.random() - 0.5) * 0.03;
var flow = 1.5 + 9 * Math.exp(-Math.pow(t - 0.58, 2) / 0.03) + Math.random() * 2;
if (i % 5 === 0) {
lng = 121.52 + (Math.random() + Math.random() - 1) * 0.05;
lat = 31.14 + (Math.random() + Math.random() - 1) * 0.04;
flow = 2 + Math.random() * 7;
}
points.push({
lng: lng,
lat: lat,
flow: flow
});
}
document.getElementById('n').textContent = points.length.toLocaleString();
var colorRange = [
[12, 26, 54, 160],
[26, 76, 132, 185],
[34, 138, 172, 200],
[110, 194, 158, 215],
[246, 204, 92, 230],
[236, 112, 54, 245]
];
var aggregation = 'SUM';
function buildLayer() {
return new deck.GridLayer({
id: 'grid',
data: points,
getPosition: function (d) { return [d.lng, d.lat]; },
cellSize: 700,
coverage: 0.86,
extruded: true,
elevationScale: 0.35,
getColorWeight: function (d) { return d.flow; },
colorAggregation: aggregation,
getElevationWeight: function (d) { return d.flow; },
elevationAggregation: 'SUM',
colorRange: colorRange,
upperPercentile: 100,
material: { ambient: 0.5, diffuse: 0.7, shininess: 16 },
pickable: true
});
}
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
initialViewState: {
longitude: 121.44,
latitude: 31.20,
zoom: 10.9,
pitch: 26,
bearing: 5,
maxPitch: 68
},
controller: true,
layers: [buildLayer()],
getTooltip: function (info) {
if (!info.object) return null;
var v = info.object.colorValue;
return info.object.count + ' 个点 · 聚合值 ' +
(v === undefined ? '—' : v.toFixed(1));
}
});
var buttons = document.querySelectorAll('[data-agg]');
for (var b = 0; b < buttons.length; b++) {
buttons[b].addEventListener('click', function (e) {
aggregation = e.currentTarget.getAttribute('data-agg');
for (var k = 0; k < buttons.length; k++) {
buttons[k].classList.toggle('on', buttons[k] === e.currentTarget);
}
deckgl.setProps({ layers: [buildLayer()] });
});
}
window.__deck = deckgl;
})();
</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.