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), ornone(allow all markers to overlap). - Priority Levels: Assign numeric
priorityvalues 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);
});
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex" />
<title>Marker collision management and priorities</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<div id="map"></div>
<div class="panel">
<strong>Collision Management</strong>
<div class="panel-desc">
Zoom in and out. Lower priority markers automatically hide or minimize to avoid overlapping.
</div>
<label for="collision-mode">Collision Behavior:</label>
<select id="collision-mode">
<option value="hide-by-priority" selected>Hide by Priority</option>
<option value="minimize-by-priority">Minimize by Priority</option>
<option value="none">None (Allow Overlap)</option>
</select>
</div>
<script type="module" src="./main.js"></script>
</body>
</html>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
.panel {
position: absolute;
top: 10px;
left: 10px;
z-index: 10;
background: rgba(255, 255, 255, 0.95);
border-radius: 8px;
padding: 10px 14px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
font-family:
system-ui,
-apple-system,
sans-serif;
font-size: 13px;
display: flex;
flex-direction: column;
gap: 8px;
max-width: 280px;
}
.panel-desc {
color: #475569;
font-size: 12px;
line-height: 1.4;
}
.panel select {
padding: 6px 10px;
border: 1px solid #cbd5e1;
border-radius: 4px;
font-size: 12px;
background: #f8fafc;
cursor: pointer;
}
Related examples
HTML clusters with custom properties
ExamplesEnhance map clustering using HTML markers and custom expressions.
Marker shapes and customization
ExamplesConfigure and create customizable, draggable markers in real-time with custom shapes, embedded letters, emojis, sizes, colors, and shadows.
Marker animations and states
ExamplesCreate animated markers with interactive UI states and transitions.