For AI agents: a documentation index is available at /llms.txt. Markdown versions of all pages are available by appending .md to any URL.

Marker collision management and priorities

This example demonstrates how to use the built-in collision detection and priority system introduced in MapTiler SDK v5:

  • Collision Behaviors: Choose between hide-by-priority (non-essential markers fade out), minimize-by-priority (non-essential markers scale down to compact dots), or none (allow all markers to overlap).
  • Priority Levels: Assign numeric priority values to markers so that high-priority landmarks (such as Big Ben or Buckingham Palace) remain prominently displayed on the map while less critical points yield when screen space is limited.
  • Minimized State Options: Customize the visual appearance of markers when minimized using minimizedOptions.

Zoom the map in and out or use the dropdown to switch between collision modes and see the markers automatically declutter.

NPM module setup


npm install --save @maptiler/sdk
import { Map, MapStyle, config, Popup, Marker } from "@maptiler/sdk";
import "@maptiler/sdk/dist/maptiler-sdk.css";

config.apiKey = "YOUR_MAPTILER_API_KEY";

const map = new Map({
  container: "map",
  style: MapStyle.STREETS,
  center: [-0.1276, 51.5074], // London
  zoom: 12.5,
});

// Dataset of locations with varying collision priorities
const spots = [
  // High Priority (Priority: 10)
  {
    name: "Big Ben",
    lng: -0.1246,
    lat: 51.5007,
    priority: 10,
    color: "#E11D48",
    size: "l",
    shape: "maptiler",
  },
  {
    name: "Tower of London",
    lng: -0.0759,
    lat: 51.5081,
    priority: 10,
    color: "#E11D48",
    size: "l",
    shape: "maptiler",
  },
  {
    name: "Buckingham Palace",
    lng: -0.1419,
    lat: 51.5014,
    priority: 10,
    color: "#E11D48",
    size: "l",
    shape: "maptiler",
  },

  // Medium Priority (Priority: 5)
  {
    name: "London Eye",
    lng: -0.1195,
    lat: 51.5033,
    priority: 5,
    color: "#0070F3",
    size: "m",
    shape: "bubble-circle",
  },
  {
    name: "St Paul's Cathedral",
    lng: -0.0984,
    lat: 51.5138,
    priority: 5,
    color: "#0070F3",
    size: "m",
    shape: "bubble-circle",
  },
  {
    name: "British Museum",
    lng: -0.1269,
    lat: 51.5194,
    priority: 5,
    color: "#0070F3",
    size: "m",
    shape: "bubble-circle",
  },
  {
    name: "Tate Modern",
    lng: -0.0994,
    lat: 51.5076,
    priority: 5,
    color: "#0070F3",
    size: "m",
    shape: "bubble-circle",
  },

  // Low Priority (Priority: 1)
  {
    name: "Covent Garden",
    lng: -0.1227,
    lat: 51.5117,
    priority: 1,
    color: "#10B981",
    size: "s",
    shape: "circle",
  },
  {
    name: "Trafalgar Square",
    lng: -0.1281,
    lat: 51.508,
    priority: 1,
    color: "#10B981",
    size: "s",
    shape: "circle",
  },
  {
    name: "Piccadilly Circus",
    lng: -0.1344,
    lat: 51.5101,
    priority: 1,
    color: "#10B981",
    size: "s",
    shape: "circle",
  },
  {
    name: "Leicester Square",
    lng: -0.1283,
    lat: 51.5113,
    priority: 1,
    color: "#10B981",
    size: "s",
    shape: "circle",
  },
  {
    name: "Westminster Abbey",
    lng: -0.1273,
    lat: 51.4994,
    priority: 1,
    color: "#10B981",
    size: "s",
    shape: "circle",
  },
];

let markers = [];

function renderMarkers(collisionBehaviour) {
  if (markers.length > 0) {
    markers.forEach((m) => m.remove());
    markers = [];
  }

  markers = spots.map((spot) => {
    return new Marker({
      shape: spot.shape,
      size: spot.size,
      innerColor: spot.color,
      outerColor: "#FFFFFF",
      priority: spot.priority,
      collisionBehaviour: collisionBehaviour,
      collisionRadius: 28,
      minimizedOptions: {
        size: "xs",
        innerColor: "#94A3B8",
        outerColor: "#FFFFFF",
      },
      shadow: "soft",
    })
      .setLngLat([spot.lng, spot.lat])
      .setPopup(
        new Popup({ offset: 25 }).setHTML(
          `<strong>${spot.name}</strong><br>Priority: ${spot.priority}`,
        ),
      );
  });

  markers.forEach((m) => map.addMarker(m));
}

map.on("load", () => {
  renderMarkers("hide-by-priority");
});

document.getElementById("collision-mode").addEventListener("change", (e) => {
  renderMarkers(e.target.value);
});
Display simple marker on the map

Display marker

Tutorials

Learn how to add a default marker to your map.

Display HTML clusters with custom properties

HTML clusters with custom properties

Examples

Enhance map clustering using HTML markers and custom expressions.

Customize and create marker shapes interactively

Marker shapes and customization

Examples

Configure and create customizable, draggable markers in real-time with custom shapes, embedded letters, emojis, sizes, colors, and shadows.

Marker animations, transitions, and interactive UI states

Marker animations and states

Examples

Create animated markers with interactive UI states and transitions.

Was this helpful?