Atlaslog
Atlaslog / Examples / MapLibre GL JS / Popup on Hover
MapLibre GL JS · Runnable Example

Popup on Hover

Runnable MapLibre GL JS example: Popup on Hover. 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-popup-on-hover.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-popup-on-hover.html
<!--
  示例:悬停弹窗
  核心逻辑:Popup 关闭默认按钮与点击关闭,mousemove 时跟随指针移动,移出图层即移除
  数据:页面内生成的 12 个营业网点
  依赖: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}
  #map{flex:1 1 auto;min-height:0}
  .pop{font:13px/1.5 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
  .pop u{text-decoration:none;color:#e8590c;font-weight:600}
</style>
</head>
<body>
<div class="bar">
  <span>网点 <b>12</b></span>
  <span>弹窗跟随 <b id="cur">—</b></span>
  <span>指针离开圆点,弹窗自动消失</span>
</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 shops = [
    ['北京中关村店', 116.31, 39.98, 320],
    ['北京望京店', 116.47, 40.00, 180],
    ['上海徐汇店', 121.44, 31.19, 260],
    ['上海浦东店', 121.54, 31.24, 410],
    ['广州天河店', 113.33, 23.13, 230],
    ['深圳南山店', 113.93, 22.53, 380],
    ['成都春熙店', 104.08, 30.65, 190],
    ['西安雁塔店', 108.95, 34.22, 150],
    ['武汉光谷店', 114.40, 30.51, 210],
    ['杭州西湖店', 120.13, 30.26, 280],
    ['南京新街口店', 118.79, 32.04, 170],
    ['重庆解放碑店', 106.58, 29.56, 240]
  ];

  var geojson = {
    type: 'FeatureCollection',
    features: shops.map(function (s) {
      return {
        type: 'Feature',
        properties: { name: s[0], staff: s[3] },
        geometry: { type: 'Point', coordinates: [s[1], s[2]] }
      };
    })
  };

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

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

  var curEl = document.getElementById('cur');

  /* 悬停弹窗不要关闭按钮,也不要点击关闭,避免和点击事件冲突 */
  var hoverPopup = new maplibregl.Popup({
    closeButton: false,
    closeOnClick: false,
    offset: 14,
    maxWidth: '220px'
  });

  map.on('load', function () {
    map.addSource('shops', { type: 'geojson', data: geojson });

    map.addLayer({
      id: 'shops',
      type: 'circle',
      source: 'shops',
      paint: {
        'circle-radius': ['interpolate', ['linear'], ['get', 'staff'], 140, 6, 420, 12],
        'circle-color': '#e8590c',
        'circle-opacity': 0.85,
        'circle-stroke-width': 2,
        'circle-stroke-color': '#ffffff'
      }
    });
  });

  map.on('mousemove', 'shops', function (e) {
    if (!e.features.length) return;
    var f = e.features[0];
    var p = f.properties;

    map.getCanvas().style.cursor = 'pointer';
    curEl.textContent = p.name;

    hoverPopup
      .setLngLat(e.lngLat)
      .setHTML(
        '<div class="pop"><u>' + p.name + '</u><br>' +
        '店员 ' + p.staff + ' 人<br>' +
        '<span style="color:#888">' + e.lngLat.lng.toFixed(3) + ', ' +
        e.lngLat.lat.toFixed(3) + '</span></div>'
      )
      .addTo(map);
  });

  map.on('mouseleave', 'shops', function () {
    map.getCanvas().style.cursor = '';
    curEl.textContent = '—';
    hoverPopup.remove();
  });

  map.on('error', function (e) {
    if (e.error) console.warn('地图错误:' + 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 · EXAMPLERaster Tiles MapLibre GL JS · EXAMPLERealtime Stream MapLibre GL JS · EXAMPLESky Layer