MapLibre GL: Add a custom marker image to a symbol layer

MapLibre GL can add an image to the map style and draw many points with it as a symbol layer. For hundreds of points, this is faster than separate HTML markers, and one image serves every point.

In this code sample, we load one Geoapify Marker Icon API pin into the style and show six Vienna coffee houses with it. The full code is on CodePen and in the quickstart examples on GitHub.

1. Get the marker image and its anchor

Design a marker in the Marker Icon API playground or set the parameters in code. Request it once with fetch() and read the pin-tip position from the Icon-* response headers:

const SCALE_FACTOR = 2;   // double-resolution PNG for sharp markers
const markerUrl = "https://api.geoapify.com/v2/icon?type=awesome&icon=mug-hot&color=%23ff9999&contentColor=%23ffffff&noWhiteCircle&size=48&contentSize=20&scaleFactor=2&apiKey=YOUR_API_KEY";

const response = await fetch(markerUrl);
const anchorX = Number(response.headers.get("Icon-Anchor-X"));   // 18.5 for this marker
const anchorY = Number(response.headers.get("Icon-Anchor-Y"));   // 48.37
const imageUrl = URL.createObjectURL(await response.blob());

2. Add the image to the map style

Use map.loadImage() and map.addImage(). Set pixelRatio to the scaleFactor of the marker URL, so the image is drawn at its display size:

map.on("load", async () => {
  // ... fetch the marker as in step 1
  const image = await map.loadImage(imageUrl);
  map.addImage("cafe-marker", image.data, { pixelRatio: SCALE_FACTOR });
  // ... add the source and the layer (step 3)
});

3. Use the image in a symbol layer

Set the image as the layer's icon-image. The named icon-anchor positions don't include the pin tip above a shadow, so anchor the image at its top-left corner and shift it back by the anchor from the headers:

map.addSource("cafes", { type: "geojson", data: cafes });   // a FeatureCollection of points

map.addLayer({
  id: "cafes",
  type: "symbol",
  source: "cafes",
  layout: {
    "icon-image": "cafe-marker",
    "icon-anchor": "top-left",
    "icon-offset": [-anchorX, -anchorY],
    "icon-allow-overlap": true
  }
});

MapLibre GL map of central Vienna with six pink coffee-cup pins drawn by one symbol layer from a single Marker Icon API image: Café Central, Sacher, Landtmann, Hawelka, Sperl, and Prückel

Rendered with MapLibre GL 5.24.0 and the Geoapify osm-bright-grey vector style.

4. Open a popup on click

Layers have map events. Listen to click on the layer to show the clicked point's name, and change the cursor on hover:

map.on("click", "cafes", (event) => {
  const feature = event.features[0];
  new maplibregl.Popup({ anchor: "bottom", offset: [0, -anchorY] })
    .setLngLat(feature.geometry.coordinates)
    .setText(feature.properties.name)
    .addTo(map);
});

map.on("mouseenter", "cafes", () => { map.getCanvas().style.cursor = "pointer"; });
map.on("mouseleave", "cafes", () => { map.getCanvas().style.cursor = ""; });

A click on the Café Hawelka marker opens a popup with the café's name above the pin

The icons are from Font Awesome Free, so the map credits them in its attribution. See Shadows and map placement for other shadow styles.