Picking
Runnable deck.gl example: Picking. 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 拾取高亮
要点:ScatterplotLayer 开启拾取,点击后用环形与光晕图层标出选中点,并同步信息面板
依赖: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:16px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.stage{position:relative;flex:1 1 auto;min-height:0}
#cv{width:100%;height:100%;display:block}
.panel{position:absolute;left:14px;bottom:14px;min-width:190px;padding:10px 14px;
background:rgba(16,22,32,0.9);border:1px solid #2c3644;border-radius:8px;
font-size:12px;line-height:1.9;color:#9aa4b4;pointer-events:none}
.panel .hd{color:#e6e8ec;font-weight:600;margin-bottom:2px}
.panel .val{color:#7fc4ff;font-family:ui-monospace,Consolas,monospace}
.hint{position:absolute;right:14px;bottom:14px;font-size:11px;color:#5f6875}
</style>
</head>
<body>
<div class="bar">
<span>点位 <b id="n">—</b></span>
<span>点击任意点选中 · 点击空白取消</span>
</div>
<div class="stage">
<canvas id="cv"></canvas>
<div class="panel" id="panel">
<div class="hd">未选中</div>
<div>点击画面中的点查看详情</div>
</div>
<div class="hint">悬停可预览坐标</div>
</div>
<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 data = [];
for (var i = 0; i < 6000; i++) {
data.push({
id: 'P' + (1000 + i),
lng: 121.46 + (Math.random() + Math.random() - 1) * 0.12,
lat: 31.22 + (Math.random() + Math.random() - 1) * 0.09,
flow: Math.round(20 + Math.random() * 480),
level: Math.random() > 0.82 ? 2 : 1
});
}
document.getElementById('n').textContent = data.length;
var selected = null;
var panel = document.getElementById('panel');
function colorOf(d) {
if (d.level === 2) return [255, 140, 84, 215];
return [92, 178, 255, 205];
}
var pointsLayer = new deck.ScatterplotLayer({
id: 'points',
data: data,
getPosition: function (d) { return [d.lng, d.lat]; },
getRadius: function (d) { return d.level === 2 ? 4 : 2.4; },
radiusUnits: 'pixels',
radiusMinPixels: 2,
radiusMaxPixels: 8,
getFillColor: colorOf,
pickable: true,
autoHighlight: true,
highlightColor: [255, 255, 255, 90]
});
function haloLayer() {
return new deck.ScatterplotLayer({
id: 'halo',
data: selected ? [selected] : [],
getPosition: function (d) { return [d.lng, d.lat]; },
getRadius: 30,
radiusUnits: 'pixels',
getFillColor: [90, 190, 255, 55],
pickable: false
});
}
function ringLayer() {
return new deck.ScatterplotLayer({
id: 'ring',
data: selected ? [selected] : [],
getPosition: function (d) { return [d.lng, d.lat]; },
getRadius: 17,
radiusUnits: 'pixels',
filled: false,
stroked: true,
getLineColor: [255, 255, 255, 235],
getLineWidth: 2,
lineWidthUnits: 'pixels',
pickable: false
});
}
function renderPanel() {
if (!selected) {
panel.innerHTML = '<div class="hd">未选中</div><div>点击画面中的点查看详情</div>';
return;
}
panel.innerHTML =
'<div class="hd">' + selected.id + '</div>' +
'<div>经度 <span class="val">' + selected.lng.toFixed(5) + '</span></div>' +
'<div>纬度 <span class="val">' + selected.lat.toFixed(5) + '</span></div>' +
'<div>流量 <span class="val">' + selected.flow + '</span></div>' +
'<div>等级 <span class="val">' + (selected.level === 2 ? '重点' : '普通') + '</span></div>';
}
var deckgl = new deck.Deck({
canvas: document.getElementById('cv'),
width: '100%',
height: '100%',
initialViewState: {
longitude: 121.46,
latitude: 31.22,
zoom: 11.2,
pitch: 38,
bearing: -10,
maxPitch: 65
},
controller: true,
layers: [pointsLayer, haloLayer(), ringLayer()],
getTooltip: function (info) {
if (!info.object || !info.object.id) return null;
return info.object.id + ' · 流量 ' + info.object.flow;
},
onClick: function (info) {
selected = info.object && info.object.id ? info.object : null;
renderPanel();
deckgl.setProps({ layers: [pointsLayer, haloLayer(), ringLayer()] });
}
});
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.