Atlaslog
Atlaslog / Examples / MapLibre GL JS / Fit Bounds
MapLibre GL JS · Runnable Example

Fit Bounds

Runnable MapLibre GL JS example: Fit Bounds. 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-fit-bounds.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-fit-bounds.html
<!--
  示例:缩放到范围
  核心逻辑:fitBounds 让指定矩形完整进入视野,动画结束后回读实际范围
  依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 免 Key 样式
-->
<!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:8px;align-items:center;padding:8px 12px;
       background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar button{font:inherit;font-size:12px;padding:4px 10px;border-radius:4px;cursor:pointer;
       border:1px solid #ccd1d9;background:#fff;color:#16181d}
  .bar button:hover{background:#eef0f3}
  .bar b{color:#16181d;font-weight:600}
  .out{flex:0 0 auto;padding:6px 12px;border-top:1px solid #e3e6ea;
       font-family:ui-monospace,Consolas,monospace;font-size:11.5px;color:#3a4250}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <button data-box="eastasia">东亚</button>
  <button data-box="europe">欧洲</button>
  <button data-box="africa">非洲</button>
  <button data-box="southamerica">南美</button>
</div>
<div class="out">当前范围 <b id="b">—</b></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 boxes = {
    eastasia: [[95, 15], [145, 50]],
    europe: [[-12, 34], [40, 60]],
    africa: [[-18, -35], [52, 37]],
    southamerica: [[-82, -56], [-34, 12]]
  };

  var map = new maplibregl.Map({
    container: 'map',
    style: 'https://demotiles.maplibre.org/style.json',
    center: [104, 34],
    zoom: 2.2,
    attributionControl: {
      compact: true,
      customAttribution: 'MapLibre demo tiles'
    }
  });

  function showBounds() {
    var b = map.getBounds();
    document.getElementById('b').textContent =
      b.getWest().toFixed(2) + ', ' + b.getSouth().toFixed(2) + ' → ' +
      b.getEast().toFixed(2) + ', ' + b.getNorth().toFixed(2);
  }

  document.querySelectorAll('[data-box]').forEach(function (btn) {
    btn.addEventListener('click', function () {
      map.fitBounds(boxes[this.getAttribute('data-box')], {
        padding: 36,
        duration: 1400
      });
    });
  });

  map.on('moveend', showBounds);
  map.on('load', showBounds);

  map.addControl(new maplibregl.NavigationControl(), '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 · EXAMPLEFly to MapLibre GL JS · EXAMPLEGeoJSON Line MapLibre GL JS · EXAMPLEGeoJSON Points