Terrain
Runnable MapLibre GL JS example: Terrain. 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
<!--
示例:地形与晕渲
核心逻辑:raster-dem 源 + setTerrain 打开地形,hillshade 图层补上光影,滑块调夸张系数
依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式 · AWS 高程瓦片
-->
<!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>
<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}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<label>夸张系数 <input id="ex" type="range" min="1" max="2.4" step="0.1" value="1.4"> <b id="exv">1.4</b></label>
<label><input id="hs" type="checkbox" checked> 晕渲</label>
<label><input id="on" type="checkbox" checked> 开启地形</label>
<span id="msg">拖动查看山脊起伏</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 map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [86.9, 27.9],
zoom: 4.2,
pitch: 60,
bearing: -22,
maxPitch: 85,
attributionControl: {
compact: true,
customAttribution: 'MapLibre demo tiles · Elevation: AWS Terrain Tiles'
}
});
map.addControl(new maplibregl.NavigationControl({ visualizePitch: true }), 'top-right');
var errors = 0;
map.on('error', function (e) {
errors++;
if (errors === 6) {
document.getElementById('msg').textContent =
'高程瓦片可能无法访问,地形未生效,底图仍可操作';
}
if (e.error) console.warn('地图错误:' + e.error.message);
});
map.on('load', function () {
map.addSource('dem', {
type: 'raster-dem',
tiles: ['https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png'],
tileSize: 256,
encoding: 'terrarium',
maxzoom: 12,
attribution: 'Elevation: AWS Terrain Tiles'
});
map.addLayer({
id: 'hillshade',
type: 'hillshade',
source: 'dem',
paint: {
'hillshade-exaggeration': 0.5,
'hillshade-shadow-color': '#2b3440'
}
});
map.setTerrain({ source: 'dem', exaggeration: 1.4 });
});
document.getElementById('ex').addEventListener('input', function () {
var v = parseFloat(this.value);
document.getElementById('exv').textContent = v.toFixed(1);
if (map.getSource('dem') && document.getElementById('on').checked) {
map.setTerrain({ source: 'dem', exaggeration: v });
}
});
document.getElementById('hs').addEventListener('change', function () {
if (map.getLayer('hillshade')) {
map.setLayoutProperty('hillshade', 'visibility', this.checked ? 'visible' : 'none');
}
});
document.getElementById('on').addEventListener('change', function () {
if (!map.getSource('dem')) return;
if (this.checked) {
map.setTerrain({
source: 'dem',
exaggeration: parseFloat(document.getElementById('ex').value)
});
} else {
map.setTerrain(null);
}
});
})();
</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.