Atlaslog
Atlaslog / Examples / Leaflet / Scale Control
Leaflet · Runnable Example

Scale Control

Runnable Leaflet example: Scale Control. 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-scale-control.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/leaflet-scale-control.html
<!--
  示例:比例尺控件
  要点:L.control.scale 的 metric / imperial / maxWidth 组合、按像素实测距离校验比例尺
  依赖: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 button.on{background:#1f5fd0;border-color:#1f5fd0;color:#fff}
  .bar b{color:#16181d;font-weight:600;font-variant-numeric:tabular-nums}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <button data-mode="metric" class="on">只公制</button>
  <button data-mode="imperial">只英制</button>
  <button data-mode="both">公制 + 英制</button>
  <button data-mode="narrow">窄版 60px</button>
  <span>100 像素 ≈ <b id="dist">—</b></span>
  <span>中心纬度 <b id="lat">—</b></span>
</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: 12, zoomSnap: 1 });

  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 distEl = document.getElementById('dist');
  var latEl = document.getElementById('lat');

  var scaleA = null;
  var scaleB = null;

  function clearScales() {
    if (scaleA) { map.removeControl(scaleA); scaleA = null; }
    if (scaleB) { map.removeControl(scaleB); scaleB = null; }
  }

  function apply(mode) {
    clearScales();

    if (mode === 'metric') {
      scaleA = L.control.scale({
        position: 'bottomleft', maxWidth: 100, metric: true, imperial: false
      }).addTo(map);
    } else if (mode === 'imperial') {
      scaleA = L.control.scale({
        position: 'bottomleft', maxWidth: 100, metric: false, imperial: true
      }).addTo(map);
    } else if (mode === 'both') {
      scaleA = L.control.scale({
        position: 'bottomleft', maxWidth: 120, metric: true, imperial: true
      }).addTo(map);
    } else {
      scaleA = L.control.scale({
        position: 'bottomleft', maxWidth: 60, metric: true, imperial: false
      }).addTo(map);
      /* 另一个角再放一条,验证多控件互不影响 */
      scaleB = L.control.scale({
        position: 'bottomright', maxWidth: 100, metric: true, imperial: true
      }).addTo(map);
    }

    refresh();
  }

  function refresh() {
    var mid = map.getSize().divideBy(2);
    var a = map.containerPointToLatLng(mid);
    var b = map.containerPointToLatLng(mid.add(L.point(100, 0)));
    var m = map.distance(a, b);

    distEl.textContent = m >= 1000 ? (m / 1000).toFixed(2) + ' km' : Math.round(m) + ' m';
    latEl.textContent = map.getCenter().lat.toFixed(2) + '°';
  }

  map.on('moveend zoomend', refresh);

  Array.prototype.forEach.call(document.querySelectorAll('[data-mode]'), function (btn) {
    btn.addEventListener('click', function () {
      Array.prototype.forEach.call(document.querySelectorAll('[data-mode]'), function (b) {
        b.classList.remove('on');
      });
      btn.classList.add('on');
      apply(btn.getAttribute('data-mode'));
    });
  });

  apply('metric');
})();
</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 · EXAMPLETile Cache Leaflet · EXAMPLETile Filter Leaflet · EXAMPLETrack Playback