Fill Extrusion
Runnable MapLibre GL JS example: Fill Extrusion. 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
<!--
示例:3D 挤出
核心逻辑:fill-extrusion 图层按属性算高度与颜色,俯仰 55 度观察体块,滑块统一调高
依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式
-->
<!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>3D 挤出</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
.bar{flex:0 0 auto;display:flex;gap:12px;align-items:center;padding:8px 12px;
background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar b{color:#16181d;font-weight:600}
.bar input[type=range]{width:130px}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<label>高度倍数 <input id="k" type="range" min="0.5" max="2.5" step="0.1" value="1"> <b id="kv">1.0</b></label>
<span>最高 <b>120 m</b> · 俯仰 <b id="p">—</b></span>
<span>拖动右键改变俯仰与方向</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.js"></script>
<script>
(function () {
'use strict';
if (typeof maplibregl === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#b42318">MapLibre 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var blocks = [];
var rows = 7, cols = 7;
for (var r = 0; r < rows; r++) {
for (var c = 0; c < cols; c++) {
var x0 = 121.40 + c * 0.012;
var y0 = 31.18 + r * 0.010;
var h = 20 + ((r * cols + c) % 9) * 12;
if ((r + c) % 4 === 0) h += 24;
blocks.push({
type: 'Feature',
properties: { height: h },
geometry: {
type: 'Polygon',
coordinates: [[
[x0, y0],
[x0 + 0.008, y0],
[x0 + 0.008, y0 + 0.007],
[x0, y0 + 0.007],
[x0, y0]
]]
}
});
}
}
var map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [121.445, 31.215],
zoom: 13.2,
pitch: 55,
bearing: -18,
maxPitch: 85,
attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
});
map.addControl(new maplibregl.NavigationControl({ visualizePitch: true }), 'top-right');
function heightExpr(k) {
return ['*', ['get', 'height'], k];
}
map.on('load', function () {
map.addSource('blocks', {
type: 'geojson',
data: { type: 'FeatureCollection', features: blocks }
});
map.addLayer({
id: 'blocks',
type: 'fill-extrusion',
source: 'blocks',
paint: {
'fill-extrusion-height': heightExpr(1),
'fill-extrusion-base': 0,
'fill-extrusion-opacity': 0.85,
'fill-extrusion-color': [
'interpolate', ['linear'], ['get', 'height'],
20, '#b6d3ea',
80, '#4a90c9',
130, '#1f5fd0'
]
}
});
});
document.getElementById('k').addEventListener('input', function () {
var v = parseFloat(this.value);
document.getElementById('kv').textContent = v.toFixed(1);
if (map.getLayer('blocks')) {
map.setPaintProperty('blocks', 'fill-extrusion-height', heightExpr(v));
}
});
map.on('moveend', function () {
document.getElementById('p').textContent = map.getPitch().toFixed(0) + '°';
});
map.on('error', function (e) {
if (e.error) console.warn('地图错误:' + e.error.message);
});
})();
</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.