Atlaslog
Atlaslog / Examples / Leaflet / Draw Circle
Leaflet · Runnable Example

Draw Circle

Runnable Leaflet example: Draw Circle. 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 Leaflet examples
/demos/leaflet-draw-circle.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/leaflet-draw-circle.html
<!--
  示例:圆形绘制
  要点:两次点击定圆心与半径、mousemove 实时预览、误导半径的丢弃与标注
  依赖:Leaflet 1.9.4 · 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/leaflet@1.9.4/dist/leaflet.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:8px;align-items:center;flex-wrap:wrap;font-size:12px;color:#58606e}
  .bar button{font:inherit;font-size:12px;padding:4px 10px;border-radius:4px;cursor:pointer;
       border:1px solid #ccd1d9;background:#fff;color:#16181d}
  .bar button:hover{background:#eef0f3}
  .bar b{color:#16181d;font-weight:600;font-variant-numeric:tabular-nums}
  #map{flex:1 1 auto;min-height:0;cursor:crosshair}
</style>
</head>
<body>
<div class="bar">
  <span id="hint">第 1 次点击定圆心,移动鼠标看半径,第 2 次点击落定</span>
  <span>半径 <b id="radius">—</b></span>
  <span>已画 <b id="count">0</b> 个</span>
  <button id="clear">清空</button>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.js"></script>
<script>
(function () {
  'use strict';

  var map = L.map('map', { center: [39.907359, 116.391263], zoom: 13 });

  L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', {
    maxZoom: 19,
    attribution: 'Tiles © Esri'
  }).addTo(map);

  var circles = L.layerGroup().addTo(map);
  var centers = L.layerGroup().addTo(map);
  var stage = 0;          /* 0 等待圆心,1 等待半径 */
  var center = null;
  var centerMark = null;
  var temp = null;

  var hintEl = document.getElementById('hint');
  var radiusEl = document.getElementById('radius');
  var countEl = document.getElementById('count');

  function fmt(r) {
    return r >= 1000 ? (r / 1000).toFixed(2) + ' km' : Math.round(r) + ' m';
  }

  function cancel() {
    if (temp) map.removeLayer(temp);
    if (centerMark) centers.removeLayer(centerMark);
    temp = null;
    centerMark = null;
    center = null;
    stage = 0;
    radiusEl.textContent = '—';
    hintEl.textContent = '第 1 次点击定圆心,移动鼠标看半径,第 2 次点击落定';
  }

  map.on('click', function (e) {
    if (stage === 0) {
      center = e.latlng;
      temp = L.circle(center, {
        radius: 20,
        color: '#17694a',
        weight: 2,
        dashArray: '5 4',
        fillColor: '#22c55e',
        fillOpacity: 0.10
      }).addTo(map);

      centerMark = L.circleMarker(center, {
        radius: 3, color: '#fff', weight: 1, fillColor: '#17694a', fillOpacity: 1
      }).addTo(centers).bindTooltip('圆心', { direction: 'top' });

      stage = 1;
      hintEl.textContent = '移动鼠标调整半径,再次点击确认';
      return;
    }

    var r = center.distanceTo(e.latlng);

    if (r < 20) {
      cancel();
      hintEl.textContent = '半径太小,已丢弃,请重新定圆心';
      return;
    }

    /* 落定:去掉虚线,改成实线并加常驻标注 */
    temp.setStyle({ dashArray: null, fillOpacity: 0.14 });
    temp.bindTooltip('半径 ' + fmt(r) + ' · 面积 ' +
      (Math.PI * r * r / 1e6).toFixed(2) + ' km²',
      { permanent: true, direction: 'center', opacity: 0.95 });

    circles.addLayer(temp);
    countEl.textContent = circles.getLayers().length;

    temp = null;
    centerMark = null;
    center = null;
    stage = 0;
    radiusEl.textContent = '—';
    hintEl.textContent = '第 1 次点击定圆心,移动鼠标看半径,第 2 次点击落定';
  });

  map.on('mousemove', function (e) {
    if (stage !== 1 || !temp) return;
    var r = center.distanceTo(e.latlng);
    temp.setRadius(r);
    radiusEl.textContent = fmt(r);
  });

  document.addEventListener('keydown', function (e) {
    if (e.key === 'Escape' && stage === 1) cancel();
  });

  document.getElementById('clear').addEventListener('click', function () {
    circles.clearLayers();
    centers.clearLayers();
    countEl.textContent = '0';
    cancel();
  });
})();
</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 Leaflet examples

Leaflet · EXAMPLEDraw Polygon Leaflet · EXAMPLEEvent Debug Leaflet · EXAMPLEExport Image