Atlaslog
Atlaslog / Examples / MapLibre GL JS / Center on Click
MapLibre GL JS · Runnable Example

Center on Click

Runnable MapLibre GL JS example: Center on Click. 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-center-on-click.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-center-on-click.html
<!--
  示例:点击符号居中
  核心逻辑:点击命中符号后,把该要素坐标交给 flyTo,带动画缩放到城市级别
  数据:页面内写死的 18 个城市点位
  依赖: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}
</style>
</head>
<body>
<div class="bar">
  <span>目标 <b id="cur">—</b></span>
  <span>缩放 <b id="z">3.20</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 raw = [
    ['北京', 116.39, 39.90], ['上海', 121.47, 31.23], ['广州', 113.26, 23.12],
    ['深圳', 114.06, 22.54], ['成都', 104.06, 30.57], ['重庆', 106.55, 29.56],
    ['武汉', 114.30, 30.59], ['西安', 108.94, 34.34], ['杭州', 120.15, 30.28],
    ['南京', 118.80, 32.06], ['沈阳', 123.43, 41.80], ['青岛', 120.38, 36.07],
    ['昆明', 102.71, 25.04], ['厦门', 118.09, 24.48], ['哈尔滨', 126.64, 45.75],
    ['乌鲁木齐', 87.61, 43.82], ['拉萨', 91.11, 29.65], ['海口', 110.20, 20.04]
  ];

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

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

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

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

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

    /* 圆点负责接点击,文字只做展示,避免文字命中范围太小 */
    map.addLayer({
      id: 'cities-dot',
      type: 'circle',
      source: 'cities',
      paint: {
        'circle-radius': 6,
        'circle-color': '#2e86de',
        'circle-opacity': 0.9,
        'circle-stroke-width': 1.5,
        'circle-stroke-color': '#ffffff'
      }
    });

    map.addLayer({
      id: 'cities-label',
      type: 'symbol',
      source: 'cities',
      layout: {
        'text-field': ['get', 'name'],
        'text-font': ['Noto Sans Regular'],
        'text-size': 12,
        'text-offset': [0, 1.2],
        'text-anchor': 'top'
      },
      paint: {
        'text-color': '#1f2d3d',
        'text-halo-color': '#ffffff',
        'text-halo-width': 1.4
      }
    });
  });

  map.on('click', 'cities-dot', function (e) {
    var f = e.features[0];
    var c = f.geometry.coordinates;

    curEl.textContent = f.properties.name;

    /* 目标缩放控制在底图能看清的范围内,避免放大后只剩大片色块 */
    map.flyTo({
      center: c,
      zoom: 6,
      speed: 1.2,
      curve: 1.5,
      essential: true
    });
  });

  map.on('mouseenter', 'cities-dot', function () {
    map.getCanvas().style.cursor = 'pointer';
  });

  map.on('mouseleave', 'cities-dot', function () {
    map.getCanvas().style.cursor = '';
  });

  map.on('move', function () {
    zEl.textContent = map.getZoom().toFixed(2);
  });

  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 · EXAMPLECluster MapLibre GL JS · EXAMPLECustom Control MapLibre GL JS · EXAMPLECustom Icon