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 animations, transitions, and interactive UI states

This example demonstrates how to configure lifecycle animations, property transitions, and interactive UI states on MapTiler Marker in SDK v5:

  • Interactive UI states: Declarative styling for hover (scale up and shadow), focus (custom outline on click or Tab navigation), active (shape morphs to square and inner color turns red while pressed), and dragging (scale up and inner color change during drag).
  • Pinned captions: Labels positioned under each showcase marker using lightweight DOM markers with pointer-events: none.
  • Lifecycle animations: Smooth marker appearance on addition (enter: { preset: "drop", duration: 500 }) and removal (exit: { preset: "fade", duration: 300 }).
  • Property transitions: Fluid interpolation of properties such as scale and inner color when switching between states (transitions: { scale: { duration: 250, easing: "ease-out" } }).
  • Dynamic controls: Click Drop Marker to drop a new animated marker at the map center, or click Remove to trigger the smooth exit animation.

Interact with each marker directly to preview its state, or use the panel controls to drop, remove, and reset markers.

NPM module setup


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

config.apiKey = "YOUR_MAPTILER_API_KEY";

const CENTER = [14.42, 50.08];
const SPACING = 0.0042;

const map = new Map({
  container: "map",
  style: MapStyle.STREETS,
  center: CENTER,
  zoom: 15,
});

// Pinned caption under each showcase marker
function addCaption(map, lngLat, text) {
  const captionEl = document.createElement("div");
  captionEl.textContent = text;
  captionEl.style.font = "700 10px system-ui, -apple-system, sans-serif";
  captionEl.style.letterSpacing = "0.03em";
  captionEl.style.color = "rgba(0, 0, 0, 0.75)";
  captionEl.style.background = "rgba(255, 255, 255, 0.85)";
  captionEl.style.padding = "2px 6px";
  captionEl.style.borderRadius = "4px";
  captionEl.style.whiteSpace = "nowrap";
  captionEl.style.pointerEvents = "none";
  captionEl.style.boxShadow = "0 1px 3px rgba(0, 0, 0, 0.1)";

  const caption = new Marker({
    element: captionEl,
    anchor: "top",
    offset: [0, 34],
    htmlAttributes: { tabindex: "-1" },
  });
  caption.setLngLat(lngLat);
  map.addMarker(caption);
  return caption;
}

// Track active markers and captions
let showcaseItems = [];
const dynamicMarkers = [];

function removeMarker(marker) {
  const dynIndex = dynamicMarkers.indexOf(marker);
  if (dynIndex !== -1) {
    dynamicMarkers.splice(dynIndex, 1);
  }

  const showcaseIndex = showcaseItems.findIndex((item) => item.marker === marker);
  if (showcaseIndex !== -1) {
    const [item] = showcaseItems.splice(showcaseIndex, 1);
    if (item.caption) {
      item.caption.remove();
    }
  }

  marker.remove();
}

// Initial showcase markers demonstrating core UI states
const showcaseDefinitions = [
  {
    caption: "HOVER",
    options: {
      content: "H",
      states: { hover: { scale: [1.35, 1.35], shadow: "strong" } },
    },
  },
  {
    caption: "FOCUS (click or Tab)",
    options: {
      content: "F",
      states: { focus: { outline: true, outlineColor: "#22c55e" } },
    },
  },
  {
    caption: "ACTIVE (press and hold)",
    options: {
      content: "A",
      states: { active: { shape: "square", innerColor: "red" } },
    },
  },
  {
    caption: "DRAGGING",
    options: {
      content: "D",
      draggable: true,
      states: { dragging: { scale: [1.5, 1.5], innerColor: "red" } },
    },
  },
];

function initShowcaseMarkers() {
  showcaseDefinitions.forEach(({ caption, options }, i) => {
    const lngLat = [CENTER[0] + (i - 1.5) * SPACING, CENTER[1]];

    const marker = new Marker({
      shape: "circle",
      size: "l",
      lngLat,
      animations: {
        enter: { preset: "drop", duration: 500 },
        exit: { preset: "fade", duration: 300 },
      },
      transitions: {
        scale: { duration: 250, easing: "ease-out" },
        innerColor: { duration: 200 },
      },
      ...options,
    });

    map.addMarker(marker);
    const captionMarker = addCaption(map, lngLat, caption);

    if (options.draggable) {
      marker.on("drag", () => {
        captionMarker.setLngLat(marker.getLngLat());
      });
    }

    // Double click showcase marker to remove it with exit animation
    marker.getElement().addEventListener("dblclick", (e) => {
      e.stopPropagation();
      removeMarker(marker);
    });

    showcaseItems.push({ marker, caption: captionMarker });
  });
}

initShowcaseMarkers();

// Button: Drop new animated marker at center
document.getElementById("btn-add").addEventListener("click", () => {
  const centerPos = map.getCenter();
  const colors = ["#0070F3", "#10B981", "#8B5CF6", "#EC4899", "#F59E0B"];
  const randomColor = colors[Math.floor(Math.random() * colors.length)];
  const jitterLng = (Math.random() - 0.5) * 0.002;
  const jitterLat = (Math.random() - 0.5) * 0.001;
  const lngLat = [centerPos.lng + jitterLng, centerPos.lat + jitterLat];

  const marker = new Marker({
    shape: "maptiler",
    size: "m",
    innerColor: randomColor,
    outerColor: "#FFFFFF",
    shadow: "medium",
    animations: {
      enter: { preset: "drop", duration: 500 },
      exit: { preset: "fade", duration: 300 },
    },
    states: {
      hover: {
        scale: [1.3, 1.3],
        shadow: "strong",
      },
      active: {
        scale: [1.1, 1.1],
      },
    },
    transitions: {
      scale: { duration: 250, easing: "ease-out" },
      innerColor: { duration: 200 },
    },
  }).setLngLat(lngLat);

  // Click directly on dropped marker to remove it with exit animation
  marker.getElement().addEventListener("click", (e) => {
    e.stopPropagation();
    removeMarker(marker);
  });

  map.addMarker(marker);
  dynamicMarkers.push(marker);
});

// Button: Remove last marker with exit animation
document.getElementById("btn-remove").addEventListener("click", () => {
  if (dynamicMarkers.length > 0) {
    const lastDynamic = dynamicMarkers[dynamicMarkers.length - 1];
    removeMarker(lastDynamic);
  } else if (showcaseItems.length > 0) {
    const lastShowcase = showcaseItems[showcaseItems.length - 1];
    removeMarker(lastShowcase.marker);
  }
});

// Button: Reset showcase markers
document.getElementById("btn-reset").addEventListener("click", () => {
  while (dynamicMarkers.length > 0) {
    const m = dynamicMarkers.pop();
    m.remove();
  }
  while (showcaseItems.length > 0) {
    const item = showcaseItems.pop();
    if (item.caption) {
      item.caption.remove();
    }
    item.marker.remove();
  }
  initShowcaseMarkers();
});
Marker lifecycle animations: enter, idle, and exit

Marker lifecycle animations

Examples

Configure enter, idle, and exit animations on map markers.

Display simple marker on the map

Display marker

Tutorials

Learn how to add a default marker to your map.

Animate a marker

Animate a marker

Examples

Animate map marker positions by updating locations each frame.

Add custom icons with markers

Add custom icons with markers

Examples

Add custom marker icons to your map.

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.

Was this helpful?