Atlaslog
Atlaslog / MapLibre GL JS / Tutorial / Icons and fonts
MapLibre GL JS · Tutorial

Icons and fonts

Quick answer

This tutorial explains how symbol layers get icons from the sprite JSON and text from glyphs PBF tiles, and how to add runtime images with map.addImage including pixelRatio and sdf options. It covers text-field, text-font, text-size and text-halo-width, collision behaviour via text-allow-overlap, and checking names with map.hasImage. You will be able to ship labels and markers that actually render.

Last verified 2026-11-03 · MapLibre GL JS 4.x · Cite this page
Key points
Example
{
  "version": 8,
  "sprite": "https://example.com/styles/basic/sprite",
  "glyphs": "https://example.com/fonts/{fontstack}/{range}.pbf",
  "sources": { /* ... */ },
  "layers": [
    {
      "id": "poi-label",
      "type": "symbol",
      "source": "poi",
      "layout": {
        "icon-image": ["get", "kind"],
        "icon-size": 0.8,
        "text-field": ["get", "name"],
        "text-font": ["Noto Sans Regular"],
        "text-size": 12,
        "text-offset": [0, 1.2]
      }
    }
  ]
}
function makeDot(color) {
  var size = 48;
  var canvas = document.createElement('canvas');
  canvas.width = canvas.height = size;
  var ctx = canvas.getContext('2d');

  ctx.beginPath();
  ctx.arc(size / 2, size / 2, size / 2 - 4, 0, Math.PI * 2);
  ctx.fillStyle = color;
  ctx.fill();
  ctx.lineWidth = 4;
  ctx.strokeStyle = '#ffffff';
  ctx.stroke();

  return ctx.getImageData(0, 0, size, size);
}

map.on('load', function () {
  for (var i = 0; i < 5; i++) {
    map.addImage('dot-' + i, makeDot('#17694a'), { pixelRatio: 2 });
  }

  map.addLayer({
    id: 'dots',
    type: 'symbol',
    source: 'sites',
    layout: {
      'icon-image': ['concat', 'dot-', ['to-string', ['get', 'level']]],
      'icon-size': 0.5,
      'icon-allow-overlap': true
    }
  });
});
Notes
Runnable examplesChinese originalAll Tutorials
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Cite this page

Atlaslog (2026). Icons and fonts. https://www.jingweizhi.com/en/maplibre/tutorials/sprite-glyphs.html (accessed 2026-11-03).