Atlaslog
Atlaslog / Examples / OpenLayers / Magnify
OpenLayers · Runnable Example

Magnify

Runnable OpenLayers example: Magnify. 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-magnify.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-magnify.html
<!--
  示例:指针放大镜 — OpenLayers magnify
  核心逻辑:指针移动时同步小地图中心,并将其定位到光标处,形成局部放大窗口
  依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!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}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:12px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
  .lens{position:absolute;width:200px;height:200px;border-radius:50%;
       border:3px solid #1f5fd0;box-shadow:0 4px 16px rgba(0,0,0,0.28);
       overflow:hidden;display:none;pointer-events:none;background:#fff;z-index:20}
</style>
</head>
<body>
<div class="bar">
  <label><input type="checkbox" id="on" checked> 启用放大镜</label>
  <label>放大倍数 <input type="range" id="factor" min="1" max="5" value="3"></label>
  <span id="info">—</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
  'use strict';

  if (typeof ol === 'undefined') {
    document.getElementById('map').innerHTML =
      '<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  function pointsLayer() {
    var features = [];
    for (var i = 0; i < 120; i++) {
      var lon = 116.34 + Math.random() * 0.10;
      var lat = 39.88 + Math.random() * 0.06;
      features.push(new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.fromLonLat([lon, lat]))
      }));
    }
    return new ol.layer.Vector({
      source: new ol.source.Vector({ features: features }),
      style: new ol.style.Style({
        image: new ol.style.Circle({
          radius: 3,
          fill: new ol.style.Fill({ color: '#c92a2a' })
        })
      })
    });
  }

  var center = ol.proj.fromLonLat([116.391, 39.907]);

  var map = new ol.Map({
    target: 'map',
    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'}) }),
      pointsLayer()
    ],
    view: new ol.View({
      center: center,
      zoom: 12,
      minZoom: 2
    }),
    controls: ol.control.defaults.defaults({ attribution: false })
  });

  /* 放大镜是一个独立的小地图,共享同一投影 */
  var lensEl = document.createElement('div');
  lensEl.className = 'lens';
  map.getViewport().appendChild(lensEl);

  var lensMap = new ol.Map({
    target: lensEl,
    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'}) }),
      pointsLayer()
    ],
    view: new ol.View({
      center: center,
      zoom: 15,
      minZoom: 2,
      maxZoom: 20
    }),
    controls: [],
    interactions: []
  });

  var factor = 3;
  var enabled = true;

  map.on('pointermove', function (evt) {
    if (evt.dragging || !enabled) return;

    var pixel = evt.pixel;
    lensEl.style.display = 'block';
    lensEl.style.left = (pixel[0] - 100) + 'px';
    lensEl.style.top = (pixel[1] - 100) + 'px';

    /* 首次显示时容器尺寸才可测,需要让地图重新取尺寸 */
    lensMap.updateSize();

    var zoom = map.getView().getZoom() || 12;
    lensMap.getView().setCenter(evt.coordinate);
    lensMap.getView().setZoom(zoom + factor);
    lensMap.render();

    var lonlat = ol.proj.toLonLat(evt.coordinate);
    document.getElementById('info').textContent =
      lonlat[0].toFixed(4) + ', ' + lonlat[1].toFixed(4) +
      ' · 放大 ' + factor + ' 级';
  });

  map.getViewport().addEventListener('pointerleave', function () {
    lensEl.style.display = 'none';
  });

  document.getElementById('on').addEventListener('change', function () {
    enabled = this.checked;
    if (!enabled) lensEl.style.display = 'none';
  });

  document.getElementById('factor').addEventListener('input', function () {
    factor = Number(this.value);
  });
})();
</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 · EXAMPLEMapbox Layer OpenLayers · EXAMPLEMapbox Style OpenLayers · EXAMPLEMapbox Vector Layer