Atlaslog
Atlaslog / Examples / MapLibre GL JS / Custom Control
MapLibre GL JS · Runnable Example

Custom Control

Runnable MapLibre GL JS example: Custom 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 MapLibre GL JS examples
/demos/maplibre-custom-control.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-custom-control.html
<!--
  示例:自定义控件
  核心逻辑:实现 onAdd/onRemove,返回一个带 maplibregl-ctrl 类名的按钮组
  依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 免 Key 样式
-->
<!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}
  .my-ctrl button{font-size:15px;line-height:1}
</style>
</head>
<body>
<div class="bar">
  <span>左上角为自定义控件</span>
  <span>视角 <b id="v">—</b></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 HOME = { center: [105, 34], zoom: 2.6, pitch: 0, bearing: 0 };

  function ViewControl() {
    this._map = null;
    this._container = null;
  }

  ViewControl.prototype.onAdd = function (map) {
    this._map = map;

    var div = document.createElement('div');
    div.className = 'maplibregl-ctrl maplibregl-ctrl-group my-ctrl';

    var reset = document.createElement('button');
    reset.type = 'button';
    reset.title = '复位';
    reset.textContent = '⌂';
    reset.addEventListener('click', function () {
      map.easeTo({ center: HOME.center, zoom: HOME.zoom, pitch: 0, bearing: 0 });
    });

    var zoomIn = document.createElement('button');
    zoomIn.type = 'button';
    zoomIn.title = '放大';
    zoomIn.textContent = '+';
    zoomIn.addEventListener('click', function () { map.zoomIn(); });

    var zoomOut = document.createElement('button');
    zoomOut.type = 'button';
    zoomOut.title = '缩小';
    zoomOut.textContent = '−';
    zoomOut.addEventListener('click', function () { map.zoomOut(); });

    div.appendChild(reset);
    div.appendChild(zoomIn);
    div.appendChild(zoomOut);

    this._container = div;
    return div;
  };

  ViewControl.prototype.onRemove = function () {
    if (this._container && this._container.parentNode) {
      this._container.parentNode.removeChild(this._container);
    }
    this._container = null;
    this._map = null;
  };

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

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

  map.on('moveend', function () {
    var c = map.getCenter();
    document.getElementById('v').textContent =
      c.lng.toFixed(2) + ', ' + c.lat.toFixed(2) + ' · z' + map.getZoom().toFixed(2);
  });
})();
</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 · EXAMPLECustom Icon MapLibre GL JS · EXAMPLEData Driven Line MapLibre GL JS · EXAMPLEDisable Rotation