Scatter Labels
Runnable deck.gl example: Scatter Labels. 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
<!--
示例:点与标签
要点:ScatterplotLayer 与 TextLayer 共用一份数据,标签用像素偏移抬高
依赖: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:14px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar label{display:flex;align-items:center;gap:5px;cursor:pointer}
.bar input[type=checkbox]{accent-color:#4f9ef8}
.bar input[type=range]{width:150px;accent-color:#4f9ef8}
.bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:4px;cursor:pointer;
border:1px solid #2f3947;background:#1d242e;color:#c9d3e0}
.bar button:hover{background:#26303d}
#cv{flex:1 1 auto;min-height:0;width:100%;display:block}
</style>
</head>
<body>
<div class="bar"><span>站点 <b id="n">—</b></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 sites = [
{ name: '国贸', lng: 116.46, lat: 39.91, v: 320 },
{ name: '中关村', lng: 116.31, lat: 39.98, v: 260 },
{ name: '望京', lng: 116.48, lat: 40.00, v: 210 },
{ name: '亦庄', lng: 116.51, lat: 39.80, v: 180 },
{ name: '西直门', lng: 116.35, lat: 39.94, v: 240 },
{ name: '五道口', lng: 116.34, lat: 39.99, v: 150 }
];
document.getElementById('n').textContent = sites.length;
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
initialViewState: { longitude: 116.41, latitude: 39.92, zoom: 11.2, pitch: 20, bearing: 0 },
controller: true,
layers: [
new deck.ScatterplotLayer({
id: 'sites',
data: sites,
getPosition: function (d) { return [d.lng, d.lat]; },
getRadius: function (d) { return Math.sqrt(d.v) / 2.2; },
radiusUnits: 'pixels',
radiusMinPixels: 3,
radiusMaxPixels: 14,
getFillColor: [255, 190, 100, 230],
stroked: true,
getLineColor: [18, 26, 40, 220],
lineWidthUnits: 'pixels',
getLineWidth: 1.5,
pickable: true
}),
new deck.TextLayer({
id: 'site-labels',
data: sites,
getText: function (d) { return d.name; },
getPosition: function (d) { return [d.lng, d.lat]; },
getSize: 12,
sizeUnits: 'pixels',
getColor: [226, 238, 250, 255],
getPixelOffset: [0, -16],
getTextAnchor: 'middle',
getAlignmentBaseline: 'bottom',
characterSet: '国贸中关村望京亦庄西直门五道口',
outlineWidth: 0.12,
outlineColor: [8, 14, 26, 230],
billboard: true,
pickable: false
})
],
getTooltip: function (info) {
if (!info.object || !info.object.v) return null;
return info.object.name + ' · ' + info.object.v;
}
});
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.