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 lifecycle animations: enter, idle, and exit

This example demonstrates how to configure lifecycle animations on MapTiler Marker in SDK v5 across three phases:

  • Enter animations: Play when a marker is added to the map. Built-in presets include drop, grow, pop, bounce, and fade, or a custom function.
  • Idle animations: Continuous looped animations while the marker stays on the map. Built-in presets include pulsescale, ring, pulseopacity, and bounce, or a custom function.
  • Exit animations: Play when a marker is removed from the map before it leaves the DOM. Built-in presets include fade, shrink, pop, and explode, or a custom function.
  • Lifecycle events: Markers emit events across each phase (enteranimationstart, enteranimationend, idleanimationiteration, exitanimationstart, exitanimationend) to coordinate application logic.

Use the dropdown controls to choose presets for each phase, then click Add Marker to test the combination or Remove to trigger the exit animation. You can also click any showcase marker directly to remove it with its exit animation.

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.0036;

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

const statusEl = document.getElementById("status");
function logEvent(text) {
  statusEl.textContent = text;
}

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;
}

function customEnter(alpha, marker) {
  marker.setScale([alpha, alpha]);
  marker.setRotation(360 * (1 - alpha));
  marker.getElement().style.opacity = String(alpha);
}

function customExit(alpha, marker) {
  marker.setScale([1 - alpha, 1 - alpha]);
  marker.setRotation(360 * alpha);
  marker.getElement().style.opacity = String(1 - alpha);
}

function customIdle(alpha, marker) {
  marker.setRotation(Math.sin(alpha * Math.PI * 2) * 15);
}

function bindLifecycleEvents(marker, label) {
  const events = [
    "enteranimationstart",
    "enteranimationend",
    "idleanimationstart",
    "idleanimationiteration",
    "exitanimationstart",
    "exitanimationend",
  ];
  events.forEach((type) => {
    marker.on(type, (e) => {
      logEvent(`${label}: ${type} (${e.preset || "custom"})`);
    });
  });
}

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();
}

const showcaseDefinitions = [
  {
    caption: "PULSESCALE",
    color: "#0070F3",
    content: "P",
    idle: { preset: "pulsescale", iterations: Infinity },
    exit: { preset: "fade", duration: 300 },
  },
  {
    caption: "RING",
    color: "#10B981",
    content: "R",
    idle: { preset: "ring", iterations: Infinity },
    exit: { preset: "fade", duration: 300 },
  },
  {
    caption: "PULSEOPACITY",
    color: "#8B5CF6",
    content: "O",
    idle: { preset: "pulseopacity", iterations: Infinity },
    exit: { preset: "fade", duration: 300 },
  },
  {
    caption: "BOUNCE",
    color: "#F59E0B",
    content: "B",
    idle: { preset: "bounce", iterations: Infinity },
    exit: { preset: "fade", duration: 300 },
  },
  {
    caption: "CUSTOM WOBBLE",
    color: "#EC4899",
    content: "C",
    idle: { custom: customIdle, duration: 1500, iterations: Infinity },
    exit: { custom: customExit, duration: 500 },
  },
];

function initShowcaseMarkers() {
  showcaseDefinitions.forEach(({ caption, color, content, idle, exit }, i) => {
    const lngLat = [CENTER[0] + (i - 2) * SPACING, CENTER[1]];

    const marker = new Marker({
      shape: "circle",
      size: "l",
      innerColor: color,
      content,
      lngLat,
      animations: {
        enter: { preset: "drop", duration: 500 + i * 100 },
        idle,
        exit,
      },
    });

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

    marker.getElement().addEventListener("click", (e) => {
      e.stopPropagation();
      removeMarker(marker);
    });

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

initShowcaseMarkers();

function buildAnimationsFromSelection() {
  const enterVal = document.getElementById("select-enter").value;
  const idleVal = document.getElementById("select-idle").value;
  const exitVal = document.getElementById("select-exit").value;

  const animations = {};

  if (enterVal === "custom") {
    animations.enter = { custom: customEnter, duration: 800 };
  } else if (enterVal !== "none") {
    animations.enter = { preset: enterVal, duration: 600 };
  }

  if (idleVal === "custom") {
    animations.idle = { custom: customIdle, duration: 1500, iterations: Infinity };
  } else if (idleVal !== "none") {
    animations.idle = { preset: idleVal, iterations: Infinity };
  }

  if (exitVal === "custom") {
    animations.exit = { custom: customExit, duration: 800 };
  } else if (exitVal !== "none") {
    animations.exit = { preset: exitVal, duration: 400 };
  }

  return animations;
}

document.getElementById("btn-add").addEventListener("click", () => {
  const centerPos = map.getCenter();
  const colors = ["#0070F3", "#10B981", "#8B5CF6", "#EC4899", "#F59E0B", "#06B6D4"];
  const randomColor = colors[Math.floor(Math.random() * colors.length)];
  const jitterLng = (Math.random() - 0.5) * 0.003;
  const jitterLat = (Math.random() - 0.5) * 0.002;
  const lngLat = [centerPos.lng + jitterLng, centerPos.lat + jitterLat];

  const animations = buildAnimationsFromSelection();

  const marker = new Marker({
    shape: "maptiler",
    size: "m",
    innerColor: randomColor,
    outerColor: "#FFFFFF",
    shadow: "medium",
    animations,
  }).setLngLat(lngLat);

  bindLifecycleEvents(marker, `Marker #${dynamicMarkers.length + 1}`);

  marker.getElement().addEventListener("click", (e) => {
    e.stopPropagation();
    removeMarker(marker);
  });

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

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);
  }
});

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();
  logEvent("Showcase markers reset");
});
Marker animations, transitions, and interactive UI states

Marker animations and states

Examples

Create animated markers with interactive UI states and transitions.

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.

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.

Was this helpful?