Atlaslog
Atlaslog / Examples / MapLibre GL JS / Pitch Bearing
MapLibre GL JS · Runnable Example

Pitch Bearing

Runnable MapLibre GL JS example: Pitch Bearing. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

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

Source

demos/maplibre-pitch-bearing.html
<!--
  示例:设置俯仰与朝向
  核心逻辑:jumpTo 直接改 pitch 与 bearing,滑块和按钮只是入口
  数据:无外部数据,读数来自相机状态
  依赖: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>设置俯仰与朝向</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:14px;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 button{background:#fff;border:1px solid #cfd6df;border-radius:4px;padding:3px 10px;
       font:12px/1.4 inherit;color:#3a4250;cursor:pointer}
  .bar button:hover{border-color:#2e86de;color:#2e86de}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <label>俯仰 <input id="p" type="range" min="0" max="75" step="1" value="45"> <b id="pv">45°</b></label>
  <label>朝向 <input id="b" type="range" min="-180" max="180" step="5" value="-20"> <b id="bv">-20°</b></label>
  <button data-preset="0,0">正射</button>
  <button data-preset="55,-30">斜视</button>
  <button data-preset="70,180">越肩</button>
</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: [104.2, 35.5],
    zoom: 3.6,
    pitch: 45,
    bearing: -20,
    maxPitch: 75,
    attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
  });

  map.addControl(new maplibregl.NavigationControl({ visualizePitch: true }), 'top-right');
  map.addControl(new maplibregl.ScaleControl({ maxWidth: 120 }), 'bottom-left');

  var pEl = document.getElementById('p');
  var bEl = document.getElementById('b');
  var pvEl = document.getElementById('pv');
  var bvEl = document.getElementById('bv');

  function apply() {
    var pitch = parseInt(pEl.value, 10);
    var bearing = parseInt(bEl.value, 10);
    pvEl.textContent = pitch + '°';
    bvEl.textContent = bearing + '°';
    map.jumpTo({ pitch: pitch, bearing: bearing });
  }

  pEl.addEventListener('input', apply);
  bEl.addEventListener('input', apply);

  /* 预设按钮把两个滑块一起拖到目标值 */
  Array.prototype.forEach.call(document.querySelectorAll('button[data-preset]'), function (btn) {
    btn.addEventListener('click', function () {
      var v = btn.getAttribute('data-preset').split(',');
      pEl.value = v[0];
      bEl.value = v[1];
      apply();
    });
  });

  /* 用户自己拖动相机后,让滑块跟随,避免控件与画面不一致 */
  map.on('move', function () {
    pEl.value = Math.round(map.getPitch());
    bEl.value = Math.round(map.getBearing());
    pvEl.textContent = Math.round(map.getPitch()) + '°';
    bvEl.textContent = Math.round(map.getBearing()) + '°';
  });

  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.

More MapLibre GL JS examples

MapLibre GL JS · EXAMPLEPolygon Layer MapLibre GL JS · EXAMPLEPolygon Pattern MapLibre GL JS · EXAMPLEPopup