Atlaslog
Atlaslog / Examples / OpenLayers / Popup
OpenLayers · Runnable Example

Popup

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

Source

demos/openlayers-popup.html
<!--
  示例:点击弹出信息框
  要点:ol.Overlay 把普通 DOM 挂到地图坐标上,配合 singleclick 填充内容
  依赖: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:14px;align-items:center;font-size:12px;color:#58606e}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
  .popup{position:relative;background:#fff;border:1px solid #d7dbe2;border-radius:8px;
         box-shadow:0 4px 16px rgba(16,24,40,.16);padding:10px 12px;min-width:170px;
         font-size:12px;line-height:1.5}
  .popup h4{margin:0 0 4px;font-size:13px}
  .popup p{margin:0;color:#58606e}
  .popup .close{position:absolute;top:4px;right:6px;border:0;background:none;
                cursor:pointer;color:#8a92a1;font-size:14px;line-height:1}
  .popup::after{content:"";position:absolute;left:50%;bottom:-7px;margin-left:-6px;
                width:10px;height:10px;background:#fff;border-right:1px solid #d7dbe2;
                border-bottom:1px solid #d7dbe2;transform:rotate(45deg)}
</style>
</head>
<body>
<div class="bar"><span>点击蓝色站点查看详情</span></div>
<div id="map"></div>
<div id="popupEl" class="popup" hidden>
  <button class="close" id="closeBtn" aria-label="关闭">×</button>
  <h4 id="pT"></h4>
  <p id="pB"></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
  'use strict';
  if (!window.ol) {
    document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
    return;
  }

  var SITES = [
    ['天安门广场', 116.391, 39.907, '城市中心广场,地面标高约 44 米'],
    ['故宫博物院', 116.397, 39.918, '明清宫殿建筑群,中轴线核心'],
    ['国家体育场', 116.397, 39.993, '奥林匹克公园内的主场馆'],
    ['颐和园', 116.275, 39.999, '皇家园林,昆明湖占全园四分之三'],
    ['首都机场', 116.600, 40.080, '东北方向的门户枢纽']
  ];

  var features = SITES.map(function (s) {
    return new ol.Feature({
      geometry: new ol.geom.Point(ol.proj.fromLonLat([s[1], s[2]])),
      name: s[0],
      info: s[3]
    });
  });

  var layer = new ol.layer.Vector({
    source: new ol.source.Vector({ features: features }),
    style: new ol.style.Style({
      image: new ol.style.Circle({
        radius: 7,
        fill: new ol.style.Fill({ color: '#1f5fd0' }),
        stroke: new ol.style.Stroke({ color: '#fff', width: 2 })
      })
    })
  });

  var popupEl = document.getElementById('popupEl');
  var overlay = new ol.Overlay({
    element: popupEl,
    positioning: 'bottom-center',
    offset: [0, -12],
    stopEvent: true
  });

  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'}) }), layer],
    overlays: [overlay],
    view: new ol.View({ center: ol.proj.fromLonLat([116.39, 39.96]), zoom: 12 }),
    controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
  });

  map.on('singleclick', function (evt) {
    var hit = null;
    map.forEachFeatureAtPixel(evt.pixel, function (f) { hit = f; return true; });
    if (!hit) {
      overlay.setPosition(undefined);
      popupEl.hidden = true;
      return;
    }
    document.getElementById('pT').textContent = hit.get('name');
    document.getElementById('pB').textContent = hit.get('info');
    popupEl.hidden = false;
    overlay.setPosition(hit.getGeometry().getCoordinates());
  });

  document.getElementById('closeBtn').addEventListener('click', function () {
    overlay.setPosition(undefined);
    popupEl.hidden = true;
  });
})();
</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 · EXAMPLEPreload OpenLayers · EXAMPLEPrint to Scale OpenLayers · EXAMPLEProjection and Scale