Atlaslog
Atlaslog / Examples / MapLibre GL JS / Terrain 3D
MapLibre GL JS · Runnable Example

Terrain 3D

Add terrain, hillshade and exaggeration to a vector-tile map.

Open standalone → Chinese notes All MapLibre GL JS examples
/demos/maplibre-terrain-3d.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-terrain-3d.html
<!--
  示例:3D 地形倾斜
  核心逻辑:raster-dem 源 + setTerrain 打开地形,俯仰 60 度观察山脊起伏
  依赖: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>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 input[type=range]{width:150px}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  .bar b{color:#16181d;font-weight:600}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <label>夸张系数 <input id="ex" type="range" min="0.6" max="2.5" step="0.1" value="1.4"> <b id="exv">1.4</b></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: 62,
    bearing: -24,
    maxPitch: 85,
    attributionControl: {
      compact: true,
      customAttribution: 'MapLibre demo tiles · Elevation: AWS Terrain Tiles'
    }
  });

  var errors = 0;

  map.on('error', function (e) {
    errors++;
    if (errors === 6) {
      document.getElementById('msg').textContent =
        '高程瓦片可能无法访问,地形未生效,底图仍可正常操作';
    }
  });

  map.on('load', function () {
    map.addSource('terrain', {
      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: 'terrain-hillshade',
      type: 'hillshade',
      source: 'terrain',
      paint: {
        'hillshade-exaggeration': 0.45,
        'hillshade-shadow-color': '#2b3440'
      }
    });

    map.setTerrain({ source: 'terrain', exaggeration: 1.4 });
  });

  document.getElementById('ex').addEventListener('input', function () {
    var v = parseFloat(this.value);
    document.getElementById('exv').textContent = v.toFixed(1);
    if (map.getSource('terrain') && document.getElementById('on').checked) {
      map.setTerrain({ source: 'terrain', exaggeration: v });
    }
  });

  document.getElementById('on').addEventListener('change', function () {
    if (!map.getSource('terrain')) return;

    if (this.checked) {
      map.setTerrain({
        source: 'terrain',
        exaggeration: parseFloat(document.getElementById('ex').value)
      });
      map.setLayoutProperty('terrain-hillshade', 'visibility', 'visible');
    } else {
      map.setTerrain(null);
      map.setLayoutProperty('terrain-hillshade', 'visibility', 'none');
    }
  });

  map.addControl(new maplibregl.NavigationControl({ visualizePitch: true }), 'top-right');
})();
</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.

More MapLibre GL JS examples

MapLibre GL JS · EXAMPLEToggle Interactions MapLibre GL JS · EXAMPLEVector Tiles MapLibre GL JS · EXAMPLEVideo Source