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

Custom Icon

Runnable MapLibre GL JS example: Custom Icon. 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-icon.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-custom-icon.html
<!--
  示例:自定义图标
  核心逻辑:canvas 画出水滴图标,addImage 注册后由 symbol 图层的 icon-image 引用
  依赖: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:12px;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}
  .bar input[type=range]{width:120px}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <label>图标大小 <input id="s" type="range" min="0.5" max="2" step="0.1" value="1"> <b id="sv">1.0</b></label>
  <label><input id="ov" type="checkbox"> 允许重叠</label>
  <span>图标由 canvas 生成并注册为 pin</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;
  }

  function makePin() {
    var c = document.createElement('canvas');
    c.width = 64;
    c.height = 64;
    var g = c.getContext('2d');

    g.beginPath();
    g.moveTo(32, 60);
    g.bezierCurveTo(10, 38, 12, 26, 32, 8);
    g.bezierCurveTo(52, 26, 54, 38, 32, 60);
    g.closePath();
    g.fillStyle = '#e8590c';
    g.fill();
    g.lineWidth = 4;
    g.strokeStyle = '#ffffff';
    g.stroke();

    g.beginPath();
    g.arc(32, 26, 7, 0, Math.PI * 2);
    g.fillStyle = '#ffffff';
    g.fill();

    return g.getImageData(0, 0, 64, 64);
  }

  var pts = {
    type: 'FeatureCollection',
    features: [
      ['Shanghai', 121.47, 31.23],
      ['Hangzhou', 120.15, 30.28],
      ['Nanjing', 118.80, 32.06],
      ['Suzhou', 120.58, 31.30],
      ['Ningbo', 121.55, 29.87],
      ['Wuhan', 114.31, 30.59]
    ].map(function (p) {
      return {
        type: 'Feature',
        properties: { name: p[0] },
        geometry: { type: 'Point', coordinates: [p[1], p[2]] }
      };
    })
  };

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

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

  map.on('load', function () {
    map.addImage('pin', makePin());

    map.addSource('poi', { type: 'geojson', data: pts });

    map.addLayer({
      id: 'poi-icon',
      type: 'symbol',
      source: 'poi',
      layout: {
        'icon-image': 'pin',
        'icon-size': 1,
        'icon-anchor': 'bottom',
        'icon-allow-overlap': false
      }
    });

    map.addLayer({
      id: 'poi-name',
      type: 'symbol',
      source: 'poi',
      layout: {
        'text-field': ['get', 'name'],
        'text-font': ['Noto Sans Regular'],
        'text-size': 12,
        'text-offset': [0, 0.4],
        'text-anchor': 'top'
      },
      paint: {
        'text-color': '#2a3f55',
        'text-halo-color': '#ffffff',
        'text-halo-width': 1.4
      }
    });
  });

  document.getElementById('s').addEventListener('input', function () {
    var v = parseFloat(this.value);
    document.getElementById('sv').textContent = v.toFixed(1);
    if (map.getLayer('poi-icon')) {
      map.setLayoutProperty('poi-icon', 'icon-size', v);
    }
  });

  document.getElementById('ov').addEventListener('change', function () {
    if (map.getLayer('poi-icon')) {
      map.setLayoutProperty('poi-icon', 'icon-allow-overlap', this.checked);
    }
  });

  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 · EXAMPLEData Driven Line MapLibre GL JS · EXAMPLEDisable Rotation MapLibre GL JS · EXAMPLEDraggable Marker