Atlaslog
Atlaslog / Examples / OpenLayers / Side By Side
OpenLayers · Runnable Example

Side By Side

Runnable OpenLayers example: Side By Side. 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 OpenLayers examples
/demos/openlayers-side-by-side.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-side-by-side.html
<!--
  示例:双图联动
  要点:两幅地图共享 center / zoom / rotation,任一视图变化同步到另一幅
  依赖:OpenLayers 10.3.1 · jsDelivr CDN(底图 OSM,无 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/ol@10.3.1/ol.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;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:16px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar b{color:#16181d;font-weight:600;font-variant-numeric:tabular-nums}
  .row{flex:1 1 auto;display:flex;min-height:0}
  .pane{flex:1 1 50%;position:relative;min-width:0}
  .pane + .pane{border-left:1px solid #dfe3e9}
  .tag{position:absolute;left:8px;top:8px;z-index:2;background:rgba(255,255,255,.9);
       border:1px solid #dfe3e9;border-radius:4px;padding:2px 7px;font-size:11px;color:#58606e}
  .map{position:absolute;inset:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <span>中心 <b id="ctr">—</b></span>
  <span>缩放 <b id="z">—</b></span>
  <span>旋转 <b id="rot">—</b></span>
  <span>拖动任一侧观察联动</span>
</div>
<div class="row">
  <div class="pane"><span class="tag">左图 · 拖动源</span><div id="mapL" class="map"></div></div>
  <div class="pane"><span class="tag">右图 · 同步跟随</span><div id="mapR" class="map"></div></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
  'use strict';
  if (!window.ol) {
    document.body.innerHTML = '<p style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问</p>';
    return;
  }

  function makeMap(target) {
    return new ol.Map({
      target: target,
      layers: [new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) })],
      view: new ol.View({
        center: ol.proj.fromLonLat([116.391, 39.907]),
        zoom: 12,
        minZoom: 2
      }),
      controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
    });
  }

  var left = makeMap('mapL');
  var right = makeMap('mapR');
  var syncing = false;

  function sync(from, to) {
    if (syncing) return;
    syncing = true;
    var v1 = from.getView();
    var v2 = to.getView();
    v2.setCenter(v1.getCenter());
    v2.setZoom(v1.getZoom());
    v2.setRotation(v1.getRotation());
    syncing = false;
  }

  ['center', 'resolution', 'rotation'].forEach(function (key) {
    left.getView().on('change:' + key, function () { sync(left, right); });
    right.getView().on('change:' + key, function () { sync(right, left); });
  });

  function readout() {
    var v = left.getView();
    var c = v.getCenter();
    var ll = ol.proj.toLonLat(c);
    document.getElementById('ctr').textContent =
      ll[0].toFixed(4) + ', ' + ll[1].toFixed(4);
    document.getElementById('z').textContent = v.getZoom().toFixed(2);
    document.getElementById('rot').textContent =
      (v.getRotation() * 180 / Math.PI).toFixed(1) + '°';
  }

  left.getView().on('change:resolution', readout);
  left.getView().on('change:center', readout);
  left.getView().on('change:rotation', readout);
  readout();
})();
</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 OpenLayers examples

OpenLayers · EXAMPLESimple OpenLayers · EXAMPLESnap OpenLayers · EXAMPLESnap Draw