<script lang="ts">
import {
Map,
MapMarker,
MapMarkerLabel,
MapMarkerPopup,
MapMarkerTooltip
} from '$lib/components/ui/map/index.js';
const CAFES = [
{ id: 'north', name: 'North Roast', lngLat: [13.398, 52.531], hours: '7:00 to 18:00' },
{ id: 'river', name: 'River Bean', lngLat: [13.412, 52.514], hours: '8:00 to 20:00' },
{ id: 'park', name: 'Park Kiosk', lngLat: [13.377, 52.517], hours: '9:00 to 17:00' }
] satisfies { id: string; name: string; lngLat: [number, number]; hours: string }[];
</script>
<Map class="size-full" viewport={{ center: [13.395, 52.521], zoom: 13, bearing: 0, pitch: 0 }}>
{#each CAFES as cafe (cafe.id)}
<MapMarker lngLat={cafe.lngLat} aria-label={cafe.name}>
<MapMarkerLabel>{cafe.name}</MapMarkerLabel>
<MapMarkerTooltip>Open {cafe.hours}</MapMarkerTooltip>
<MapMarkerPopup aria-label={`${cafe.name} details`}>
<p class="font-medium">{cafe.name}</p>
<p class="text-muted-foreground">Open {cafe.hours}</p>
</MapMarkerPopup>
</MapMarker>
{/each}
</Map>
A marker is a real <button> placed on the map. It takes focus, opens its popup with Enter or Space, and closes it with Escape, returning focus to the marker. Every marker needs an aria-label so screen readers can tell markers apart.
Use markers for up to about 200 points. For more, use a Point Layer, which draws on the canvas.
Usage
<script lang="ts">
import {
Map,
MapMarker,
MapMarkerLabel,
MapMarkerPopup,
MapMarkerTooltip
} from '$lib/components/ui/map/index.js';
</script>
<Map class="h-96">
<MapMarker lngLat={[13.405, 52.52]} aria-label="Office">
<MapMarkerLabel>Office</MapMarkerLabel>
<MapMarkerTooltip>Open 9:00 to 17:00</MapMarkerTooltip>
<MapMarkerPopup aria-label="Office details">
<p>Ring the bell at the side door.</p>
</MapMarkerPopup>
</MapMarker>
</Map>Parts
MapMarkerContentreplaces the default dot with your own markup, such as an icon or an avatar.MapMarkerLabelshows text next to the marker.MapMarkerTooltipshows on hover and focus. Screen readers read it as the marker’s description.MapMarkerPopupopens on click, Enter or Space. Focus moves into it and comes back to the marker on close.
Dragging
Set draggable and bind lngLat. The binding updates when the drag ends. ondrag fires on every move if you need the position while dragging.
<script lang="ts">
import { Map, MapMarker, MapMarkerContent } from '$lib/components/ui/map/index.js';
import MapPinIcon from '@lucide/svelte/icons/map-pin';
let pin = $state<[number, number]>([-0.1276, 51.5072]);
</script>
<div class="relative size-full">
<Map class="size-full" viewport={{ center: [-0.1276, 51.5072], zoom: 12, bearing: 0, pitch: 0 }}>
<MapMarker bind:lngLat={pin} draggable anchor="bottom" aria-label="Drop-off point">
<MapMarkerContent>
<MapPinIcon class="size-8 fill-primary text-primary-foreground drop-shadow" />
</MapMarkerContent>
</MapMarker>
</Map>
<p
class="absolute top-3 left-3 rounded-md bg-background/90 px-2 py-1 font-mono text-xs shadow"
aria-live="polite"
>
{pin[1].toFixed(4)}, {pin[0].toFixed(4)}
</p>
</div>
Moving markers
Change lngLat and the marker moves. It is never rebuilt, so an open popup stays open and focus stays put. A theme or basemap change does not remount markers either.
API Reference
MapMarker
Props
| Name | Type | Description |
|---|---|---|
lngLat required bindable | LngLatLike | Where the marker sits. Bind it to follow drags. |
aria-label required | string | Name read by screen readers. |
anchor | PositionAnchor | Which part of the marker sits on `lngLat`. MapLibre has no setter, so a change rebuilds the marker. Defaults to 'center'. |
offset | PointLike | Pixel offset from `lngLat`. Defaults to [0, 0]. |
draggable | boolean | Let users drag the marker. Bind `lngLat` to follow it. Defaults to false. |
rotation | number | Rotation in degrees, clockwise. Defaults to 0. |
rotationAlignment | Alignment | `map` turns the marker with the map, so a heading stays true; `viewport` keeps it upright on screen. Defaults to 'auto'. |
ref bindable | Marker | null | The MapLibre marker instance. Defaults to null. |
Events
| Name | Type | Description |
|---|---|---|
ondragstart | DragHandler | Called with the position when a drag starts. |
ondrag | DragHandler | Called with the position on every move while dragging. |
ondragend | DragHandler | Called with the final position when a drag ends. |
Snippets
| Name | Type | Description |
|---|---|---|
children | Snippet |
Other attributes go to the underlying <button>.
MapMarkerContent
Snippets
| Name | Type | Description |
|---|---|---|
children | Snippet | What the marker shows instead of the default dot. |
Other attributes go to the underlying <span>.
MapMarkerLabel
Props
| Name | Type | Description |
|---|---|---|
position | 'top' | 'bottom' | Side of the marker the label sits on. Defaults to 'bottom'. |
Snippets
| Name | Type | Description |
|---|---|---|
children | Snippet |
Other attributes go to the underlying <span>.
MapMarkerTooltip
Props
| Name | Type | Description |
|---|---|---|
offset | PopupOffset | Distance from the marker in pixels, or per-anchor offsets. Defaults to 14. |
maxWidth | string | CSS max-width of the tooltip. Defaults to DEFAULT_POPUP_MAX_WIDTH. |
ref bindable | Popup | null | The MapLibre popup instance behind the tooltip. Defaults to null. |
Snippets
| Name | Type | Description |
|---|---|---|
children | Snippet |
Other attributes go to the underlying <div>.
MapMarkerPopup
Props
| Name | Type | Description |
|---|---|---|
open bindable | boolean | Open state. Clicking the marker, Enter or Space toggles it. Defaults to false. |
offset | PopupOffset | Distance from the marker in pixels, or per-anchor offsets. Defaults to 18. |
maxWidth | string | CSS max-width of the popup. Defaults to DEFAULT_POPUP_MAX_WIDTH. |
closeButton | boolean | Show a close button. Read once. Defaults to true. |
ref bindable | Popup | null | The MapLibre popup instance. Defaults to null. |
Events
| Name | Type | Description |
|---|---|---|
onopen | () => void | Called when the popup opens. |
onclose | () => void | Called when the popup closes. |
Snippets
| Name | Type | Description |
|---|---|---|
children | Snippet |
Other attributes go to the underlying <div>.