Markers, Popups, and Symbol Layers

Use markers, popups, and symbol layers, and choose between DOM elements and map layers based on data volume.

🎯 Introduction

By the end of this article, you will be able to add custom markers and popups for a few locations and use symbol layers to efficiently display many icons and labels.

This article introduces markers, popups, and symbol layers in sequence, then compares their appearance in a map component.


🧱 A marker is a DOM element on the map

A marker is not a regular layer. It positions an HTML element at geographic coordinates and suits small numbers of features needing complex HTML or independent interactions.

const element = document.createElement('button')
element.className = 'map-marker'
element.textContent = 'A'
element.setAttribute('aria-label', 'View Station A')

const marker = new mapboxgl.Marker({ element })
    .setLngLat([116.397, 39.909])
    .addTo(map)

marker.setLngLat([116.407, 39.919])

function removeMarker() {
    marker.remove()
}

Style custom markers in page CSS. Many DOM markers increase layout, rendering, and event-handling costs, making them unsuitable for hundreds or thousands of application points.


💬 Connect a marker to a popup

const popup = new mapboxgl.Popup({ offset: 20 })
    .setText('Station A')

const marker = new mapboxgl.Marker()
    .setLngLat([116.397, 39.909])
    .setPopup(popup)
    .addTo(map)

popup.setText('Station A: Online')

function closePopup() {
    popup.remove()
}

setPopup toggles the popup when the marker is clicked. To render complex details with a frontend framework, create a DOM container and pass it to setDOMContent. When the page or component is destroyed, also dispose of the mounted content, marker, and popup.


🏷 Display icons and text with a symbol layer

map.loadImage('/images/place.png', (error, image) => {
    if (error || !image) return

    if (!map.hasImage('place-icon')) {
        map.addImage('place-icon', image)
    }

    map.addLayer({
        id: 'place-symbols',
        type: 'symbol',
        source: 'places',
        layout: {
            'icon-image': 'place-icon',
            'icon-size': 0.6,
            'text-field': ['get', 'name'],
            'text-offset': [0, 1.2],
            'text-size': 12,
        },
        paint: {
            'text-color': '#1f2937',
            'text-halo-color': '#ffffff',
            'text-halo-width': 1,
        },
    })
})

icon-image refers to an image registered in the style; text-field defines label content. Symbol layers render through WebGL, making them suitable for many icons and labels. They also handle symbol collisions and hiding.

The map below shows these examples together. The red dot on the left is a DOM marker with an attached popup above it. On the right, circle and symbol layers render the two purple points and their labels.


⚖️ Choose markers or symbol layers

RequirementRecommendation
A few draggable locationsMarker
A few complex HTML controlsMarker
Many application points and labelsSymbol layer
Filtering, expressions, and clusteringSymbol/circle layer
A single temporary messagePopup

Applications often combine these approaches: use layers for many features, then add a popup or a few DOM controls for the selected feature.


🧾 Summary

  • Markers are DOM elements anchored to map locations.
  • Popups can open, close, update content, and attach to markers.
  • Symbol layers use icon-image and text-field for icons and text.
  • Prefer map layers for many points instead of creating many DOM markers.
  • Small numbers of complex controls and large feature datasets can use different approaches.

❓ Questions and answers

Q1: Can markers use Mapbox expressions?

Not directly. DOM and CSS control marker styles; expressions belong to style layers.

Q2: Why aren't all symbol labels visible?

Mapbox hides colliding symbols by default. First improve text length, offsets, and zoom levels rather than immediately disabling collision detection.

Q3: Can I pass API content directly to setHTML?

Avoid doing so. Unsanitized HTML can introduce security issues. Use setText for plain text.


🧪 Exercises

  1. Create a button marker with an accessible name and attach a popup.
  2. Add a symbol layer to a point source that shows both icons and names.
// Write your code here

🎉 You now know how to choose and use markers, popups, and symbol layers!