Atlaslog
Atlaslog / Examples / MapLibre GL JS / Draggable Point
MapLibre GL JS · Runnable Example

Draggable Point

Runnable MapLibre GL JS example: Draggable Point. 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-draggable-point.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-draggable-point.html
<!--
  示例:拖动 GeoJSON 点
  核心逻辑:按下圆点关闭拖拽平移,mousemove 改写坐标并 setData,松开恢复
  数据:源里只有一个点要素,坐标随指针变化
  依赖: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>拖动 GeoJSON 点</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="pos">104.060, 30.570</b></span>
  <span>拖动次数 <b id="n">0</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 map = new maplibregl.Map({
    container: 'map',
    style: 'https://demotiles.maplibre.org/style.json',
    center: [104.06, 30.57],
    zoom: 5,
    attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
  });

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

  var feature = {
    type: 'Feature',
    properties: { name: '可拖动的点' },
    geometry: { type: 'Point', coordinates: [104.06, 30.57] }
  };

  var posEl = document.getElementById('pos');
  var countEl = document.getElementById('n');
  var drags = 0;
  var dragging = false;

  map.on('load', function () {
    map.addSource('draggable', {
      type: 'geojson',
      data: { type: 'FeatureCollection', features: [feature] }
    });

    map.addLayer({
      id: 'draggable-halo',
      type: 'circle',
      source: 'draggable',
      paint: {
        'circle-radius': 16,
        'circle-color': '#ff922b',
        'circle-opacity': 0.25
      }
    });

    map.addLayer({
      id: 'draggable-dot',
      type: 'circle',
      source: 'draggable',
      paint: {
        'circle-radius': 8,
        'circle-color': '#e8590c',
        'circle-stroke-color': '#ffffff',
        'circle-stroke-width': 2
      }
    });

    /* 悬停在点上时换成抓手光标,提示可以拖 */
    map.on('mouseenter', 'draggable-halo', function () {
      if (!dragging) map.getCanvas().style.cursor = 'grab';
    });
    map.on('mouseleave', 'draggable-halo', function () {
      if (!dragging) map.getCanvas().style.cursor = '';
    });

    map.on('mousedown', 'draggable-halo', function (e) {
      dragging = true;
      drags++;
      countEl.textContent = drags;
      map.getCanvas().style.cursor = 'grabbing';
      /* 关键一步:关掉平移,否则拖点会连底图一起动 */
      map.dragPan.disable();
      e.preventDefault();
    });
  });

  map.on('mousemove', function (e) {
    if (!dragging) return;
    var c = [e.lngLat.lng, e.lngLat.lat];
    feature.geometry.coordinates = c;
    map.getSource('draggable').setData({ type: 'FeatureCollection', features: [feature] });
    posEl.textContent = c[0].toFixed(3) + ', ' + c[1].toFixed(3);
  });

  function endDrag() {
    if (!dragging) return;
    dragging = false;
    map.getCanvas().style.cursor = '';
    map.dragPan.enable();
  }

  map.on('mouseup', endDrag);

  /* 指针在地图外松手时,画布上的 mouseup 收不到,这里兜一下 */
  window.addEventListener('mouseup', endDrag);

  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 · EXAMPLEDraw Circle MapLibre GL JS · EXAMPLEDynamic Style MapLibre GL JS · EXAMPLEFeatures Under Pointer