Atlaslog
Atlaslog / Examples / MapLibre GL JS / Map Export
MapLibre GL JS · Runnable Example

Map Export

Runnable MapLibre GL JS example: Map Export. 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-map-export.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-map-export.html
<!--
  示例:地图截图
  核心逻辑:preserveDrawingBuffer 为 true 时可直接读画布,toDataURL 导出 PNG 并生成下载链接
  依赖: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:10px;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}
  .btn{border:1px solid #cdd3db;background:#fff;border-radius:4px;padding:3px 9px;
       font:12px/1.5 inherit;color:#16181d;cursor:pointer}
  .btn[disabled]{opacity:.5;cursor:default}
  #shot{width:92px;height:64px;object-fit:cover;border:1px solid #cdd3db;border-radius:3px;
        background:#fff;display:none}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <button class="btn" id="cap">截取当前画面</button>
  <a class="btn" id="save" href="#" download="map.png" style="display:none">下载 PNG</a>
  <img id="shot" alt="截图预览">
  <span>尺寸 <b id="size">—</b></span>
  <span id="msg">截图会包含当前视角与已绘制图层</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.2, 35.5],
    zoom: 3,
    preserveDrawingBuffer: true,
    attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
  });

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

  map.on('load', function () {
    map.addSource('dots', {
      type: 'geojson',
      data: {
        type: 'FeatureCollection',
        features: [
          ['Beijing', 116.41, 39.90],
          ['Chengdu', 104.07, 30.67],
          ['Guangzhou', 113.26, 23.13],
          ['Urumqi', 87.62, 43.82]
        ].map(function (p) {
          return {
            type: 'Feature',
            properties: { name: p[0] },
            geometry: { type: 'Point', coordinates: [p[1], p[2]] }
          };
        })
      }
    });

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

  document.getElementById('cap').addEventListener('click', function () {
    var canvas = map.getCanvas();
    var url = canvas.toDataURL('image/png');

    var img = document.getElementById('shot');
    img.src = url;
    img.style.display = 'block';

    var save = document.getElementById('save');
    save.href = url;
    save.style.display = 'inline-block';

    document.getElementById('size').textContent =
      canvas.width + ' × ' + canvas.height;

    document.getElementById('msg').textContent =
      '已截取,PNG 为设备像素尺寸,高分屏会比容器大';
  });

  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 · EXAMPLEMarker MapLibre GL JS · EXAMPLEMax Bounds MapLibre GL JS · EXAMPLEMeasure Distances