Atlaslog
Atlaslog / Examples / MapLibre GL JS / Disable Rotation
MapLibre GL JS · Runnable Example

Disable Rotation

Runnable MapLibre GL JS example: Disable Rotation. 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-disable-rotation.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-disable-rotation.html
<!--
  示例:禁用地图旋转
  核心逻辑:构造时关掉 dragRotate 与 touchPitch,再单独禁用触摸旋转
  数据:无外部数据,读数来自当前相机
  依赖: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 label{display:flex;gap:5px;align-items:center;cursor:pointer}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <span>朝向 <b id="b">0°</b></span>
  <span>俯仰 <b id="p">0°</b></span>
  <label><input id="allow" type="checkbox"> 恢复旋转(演示用)</label>
  <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 map = new maplibregl.Map({
    container: 'map',
    style: 'https://demotiles.maplibre.org/style.json',
    center: [104.2, 35.5],
    zoom: 3.4,
    /* 三个开关一起关,才能覆盖鼠标、触屏与键盘三条路径 */
    dragRotate: false,
    pitchWithRotate: false,
    touchPitch: false,
    attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
  });

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

  /* 触摸缩放控件负责双指旋转,需要额外关一次 */
  map.touchZoomRotate.disableRotation();
  map.keyboard.disableRotation();

  var bEl = document.getElementById('b');
  var pEl = document.getElementById('p');

  function read() {
    bEl.textContent = Math.round(map.getBearing()) + '°';
    pEl.textContent = Math.round(map.getPitch()) + '°';
  }
  map.on('move', read);
  map.on('load', read);

  document.getElementById('allow').addEventListener('change', function () {
    var on = this.checked;
    if (on) {
      map.dragRotate.enable();
      map.touchZoomRotate.enableRotation();
      map.keyboard.enableRotation();
    } else {
      map.dragRotate.disable();
      map.touchZoomRotate.disableRotation();
      map.keyboard.disableRotation();
    }
  });

  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 · EXAMPLEDraggable Marker MapLibre GL JS · EXAMPLEDraggable Point MapLibre GL JS · EXAMPLEDraw Circle