Atlaslog
Atlaslog / Examples / MapLibre GL JS / Multi Map Sync
MapLibre GL JS · Runnable Example

Multi Map Sync

Runnable MapLibre GL JS example: Multi Map Sync. 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-multi-map-sync.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-multi-map-sync.html
<!--
  示例:多地图联动
  核心逻辑:A 地图 move 时把 center / zoom / bearing / pitch 写进 B,用标志位防回环
  依赖: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}
  .maps{flex:1 1 auto;min-height:0;display:flex}
  .pane{flex:1 1 50%;position:relative;min-width:0}
  .pane + .pane{border-left:1px solid #cdd3db}
  .pane .tag{position:absolute;left:8px;top:8px;z-index:2;background:rgba(255,255,255,.9);
             border:1px solid #cdd3db;border-radius:3px;padding:1px 7px;font-size:11px;color:#3b4654}
  #mapA,#mapB{position:absolute;inset:0}
</style>
</head>
<body>
<div class="bar">
  <label><input id="sync" type="checkbox" checked> 同步视角</label>
  <span>A 中心 <b id="ca">—</b></span>
  <span>B 中心 <b id="cb">—</b></span>
  <span>缩放差 <b id="dz">0.00</b></span>
</div>
<div class="maps">
  <div class="pane"><span class="tag">A · 可操作</span><div id="mapA"></div></div>
  <div class="pane"><span class="tag">B · 跟随</span><div id="mapB"></div></div>
</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;
  }

  function create(container, interactive) {
    return new maplibregl.Map({
      container: container,
      style: 'https://demotiles.maplibre.org/style.json',
      center: [104.2, 35.5],
      zoom: 3,
      attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' },
      interactive: interactive
    });
  }

  var mapA = create('mapA', true);
  var mapB = create('mapB', false);
  var syncing = false;

  mapA.addControl(new maplibregl.NavigationControl(), 'top-right');

  function follow() {
    if (syncing || !document.getElementById('sync').checked) return;
    syncing = true;
    mapB.jumpTo({
      center: mapA.getCenter(),
      zoom: mapA.getZoom(),
      bearing: mapA.getBearing(),
      pitch: mapA.getPitch()
    });
    syncing = false;
    report();
  }

  function report() {
    var a = mapA.getCenter();
    var b = mapB.getCenter();
    document.getElementById('ca').textContent = a.lng.toFixed(2) + ', ' + a.lat.toFixed(2);
    document.getElementById('cb').textContent = b.lng.toFixed(2) + ', ' + b.lat.toFixed(2);
    document.getElementById('dz').textContent =
      Math.abs(mapA.getZoom() - mapB.getZoom()).toFixed(2);
  }

  mapA.on('move', follow);
  mapA.on('moveend', report);
  mapB.on('moveend', report);
  mapA.on('load', report);

  document.getElementById('sync').addEventListener('change', function () {
    if (this.checked) follow();
  });

  mapA.on('error', function (e) {
    if (e.error) console.warn('A 地图错误:' + e.error.message);
  });
  mapB.on('error', function (e) {
    if (e.error) console.warn('B 地图错误:' + 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 · EXAMPLENon Interactive MapLibre GL JS · EXAMPLEOffline Cache MapLibre GL JS · EXAMPLEPadding Offset