H3 Hexagon Heatmap
30,000 points aggregate to H3 res 8 first, with hexagons extruded by count and colored on a five-step ramp; a toggle switches to a flat view.
Business context
Retail siting and market analysis teams use H3 grids for density views that compare across cities and regions, because cells at one resolution have a fixed area and multiple cities can be placed on a single map for comparison. Data comes from anonymized location sampling or footfall counts mapped point by point; a city typically has tens to hundreds of thousands of points that collapse into only a few hundred to a few thousand cells.
Before entering the layer, points must be pre-aggregated into indices with latLngToCell, and resolution must be uniform within a layer; mixing levels prevents the layer from reading resolution correctly. H3HexagonLayer is chosen over plain HexagonLayer because a fixed spherical grid is needed for cross-region alignment; turning off highPrecision uses the instanced path and saves large volumes of vertex recomputation under zoom and pan.
The most common error is passing raw points straight to getHexagon, which overplots cells repeatedly and inflates both color and height so density looks doubled. Another misconception is ignoring highPrecision: at low precision edge cells distort visibly, so boundary checks or areas with pentagons require high precision or the shapes themselves cannot be trusted.
How it works
The 30,000 points are generated around six trade areas and aggregated point by point through h3.latLngToCell(lat, lng, 8); at res 8 the hexagon edge is about 0.46 km with an area of 0.737 km², leaving a few hundred cells after aggregation, with a live count in the bar. H3HexagonLayer has no getPosition accessor, only getHexagon: coordinates must be converted to indices in JS first so the layer can read resolution from the first index, otherwise resolution is -1 and nothing renders; when only center coordinates are needed and no H3 alignment is required, HexagonLayer avoids the h3-js dependency.
With highPrecision: false the sub-layer is a ColumnLayer at diskResolution 6 and radius 1, applying the viewport-center cell's shape to every cell and recomputing vertices only when the center moves more than 10 km, which is cheap under zoom and pan. The trade-off is visible distortion on edge cells; across projections, with pentagons, or at coarse res 0–5, highPrecision: true is required and switches to PolygonLayer restoring true boundaries per cell, raising vertex counts.
| Configuration | What it does |
|---|---|
| getHexagon | Returns an H3 index string; resolution must be uniform within a layer |
| highPrecision: false | Renders instanced ColumnLayer, shape taken from the viewport-center cell |
| coverage: 0.94 | Shrinks cells toward their centers by 6%, leaving thin gaps |
| getElevation + elevationScale 1.6 | Count converted to height, so dense trade areas stand well above |
Note. Aggregate points with latLngToCell before feeding H3HexagonLayer; passing 30,000 points straight to getHexagon overplots cells and distorts both color and height.
Source
<!--
示例:H3 六边形热力
要点:8 万点位聚合到 H3 res 8 网格,highPrecision:false 走实例化柱子
依赖:h3-js 4.1 · 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>H3 六边形热力</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 label{display:flex;align-items:center;gap:5px;cursor:pointer}
.bar input[type=checkbox]{accent-color:#4f9ef8}
.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%;display:block}
</style>
</head>
<body>
<div class="bar">
<span>点位 <b id="n">—</b></span>
<span>六边形 <b id="cells">—</b></span>
<span class="sep"></span>
<button id="ex" class="on">挤出</button>
<button id="flat">平面</button>
<span class="sep"></span>
<label><input id="edge" type="checkbox" checked>显示边界</label>
<span>res 8 · 边长约 0.46 km</span>
</div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/h3-js@4.1.0/dist/h3-js.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
'use strict';
if (typeof deck === 'undefined' || typeof h3 === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#ff8c74">deck.gl 或 h3-js 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
function rng(seed) {
var a = seed >>> 0;
return function () {
a = (a + 0x6D2B79F5) >>> 0;
var t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
var HUBS = [
[116.30, 39.90, 0.032], [116.47, 39.87, 0.030],
[116.36, 39.99, 0.026], [116.53, 39.98, 0.028],
[116.24, 39.83, 0.024], [116.44, 39.94, 0.020]
];
var rand = rng(90211);
var RES = 8;
var bucket = {};
var cells = [];
var maxCount = 0;
for (var i = 0; i < 30000; i++) {
var h = HUBS[i % HUBS.length];
var lng = h[0] + (rand() + rand() + rand() - 1.5) * h[2];
var lat = h[1] + (rand() + rand() + rand() - 1.5) * h[2] * 0.8;
var hex = h3.latLngToCell(lat, lng, RES);
if (!bucket[hex]) {
bucket[hex] = { hex: hex, count: 0 };
cells.push(bucket[hex]);
}
bucket[hex].count++;
if (bucket[hex].count > maxCount) maxCount = bucket[hex].count;
}
var BAND = [
[38, 66, 110, 195],
[44, 120, 160, 208],
[70, 180, 168, 222],
[246, 196, 88, 236],
[236, 86, 58, 245]
];
function shade(d) {
var v = d.count / maxCount;
if (v < 0.16) return BAND[0];
if (v < 0.34) return BAND[1];
if (v < 0.56) return BAND[2];
if (v < 0.78) return BAND[3];
return BAND[4];
}
var extruded = true;
var showEdge = true;
function buildLayer() {
return new deck.H3HexagonLayer({
id: 'h3-density',
data: cells,
getHexagon: function (d) { return d.hex; },
highPrecision: false,
coverage: 0.94,
extruded: extruded,
getElevation: function (d) { return d.count; },
elevationScale: extruded ? 1.6 : 0,
getFillColor: shade,
filled: true,
stroked: showEdge,
getLineColor: [12, 18, 26, 220],
getLineWidth: 1,
lineWidthUnits: 'pixels',
lineWidthMinPixels: 1,
material: { ambient: 0.55, diffuse: 0.7, shininess: 16 },
pickable: true
});
}
document.getElementById('n').textContent = '30,000';
document.getElementById('cells').textContent = cells.length.toLocaleString();
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
initialViewState: {
longitude: 116.39,
latitude: 39.91,
zoom: 11.5,
pitch: 50,
bearing: -18
},
controller: true,
layers: [buildLayer()],
getTooltip: function (info) {
var o = info.object;
if (!o) return null;
return 'H3 ' + o.hex + ' · ' + o.count + ' 个点';
}
});
var exBtn = document.getElementById('ex');
var flatBtn = document.getElementById('flat');
function setExtruded(v) {
extruded = v;
exBtn.classList.toggle('on', v);
flatBtn.classList.toggle('on', !v);
deckgl.setProps({ layers: [buildLayer()] });
}
exBtn.addEventListener('click', function () { setExtruded(true); });
flatBtn.addEventListener('click', function () { setExtruded(false); });
document.getElementById('edge').addEventListener('change', function (e) {
showEdge = e.target.checked;
deckgl.setProps({ layers: [buildLayer()] });
});
window.__deck = deckgl;
})();
</script>
</body>
</html>