shadcn-svelte map
DocsComponentsBlocks
Get Started
  • Introduction
  • Installation
Guides
  • Theming
  • Drawing
  • Accessibility
  • Performance
  • Content Security Policy
Core
  • Map
  • Control Container
Markers
  • Marker
  • Popup
  • Point Layer
Data
  • GeoJSON
  • Shapes
  • Route
  • Arc
  • Cluster Layer
  • Heatmap
Controls
  • Zoom Control
  • Compass Control
  • Scale Control
  • Fullscreen Control
  • Locate Control
  • Basemap Control
  • Search Control
  • Draw Control
More
  • Place Autocomplete
  • Utilities

Marker

Previous Next

Accessible DOM markers with labels, tooltips and popups.

<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

  • MapMarkerContent replaces the default dot with your own markup, such as an icon or an avatar.
  • MapMarkerLabel shows text next to the marker.
  • MapMarkerTooltip shows on hover and focus. Screen readers read it as the marker’s description.
  • MapMarkerPopup opens 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

NameTypeDescription
lngLat required bindableLngLatLikeWhere the marker sits. Bind it to follow drags.
aria-label required stringName read by screen readers.
anchor PositionAnchorWhich part of the marker sits on `lngLat`. MapLibre has no setter, so a change rebuilds the marker. Defaults to 'center'.
offset PointLikePixel offset from `lngLat`. Defaults to [0, 0].
draggable booleanLet users drag the marker. Bind `lngLat` to follow it. Defaults to false.
rotation numberRotation 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 bindableMarker | nullThe MapLibre marker instance. Defaults to null.

Events

NameTypeDescription
ondragstart DragHandlerCalled with the position when a drag starts.
ondrag DragHandlerCalled with the position on every move while dragging.
ondragend DragHandlerCalled with the final position when a drag ends.

Snippets

NameTypeDescription
children Snippet

Other attributes go to the underlying <button>.

MapMarkerContent

Snippets

NameTypeDescription
children SnippetWhat the marker shows instead of the default dot.

Other attributes go to the underlying <span>.

MapMarkerLabel

Props

NameTypeDescription
position 'top' | 'bottom'Side of the marker the label sits on. Defaults to 'bottom'.

Snippets

NameTypeDescription
children Snippet

Other attributes go to the underlying <span>.

MapMarkerTooltip

Props

NameTypeDescription
offset PopupOffsetDistance from the marker in pixels, or per-anchor offsets. Defaults to 14.
maxWidth stringCSS max-width of the tooltip. Defaults to DEFAULT_POPUP_MAX_WIDTH.
ref bindablePopup | nullThe MapLibre popup instance behind the tooltip. Defaults to null.

Snippets

NameTypeDescription
children Snippet

Other attributes go to the underlying <div>.

MapMarkerPopup

Props

NameTypeDescription
open bindablebooleanOpen state. Clicking the marker, Enter or Space toggles it. Defaults to false.
offset PopupOffsetDistance from the marker in pixels, or per-anchor offsets. Defaults to 18.
maxWidth stringCSS max-width of the popup. Defaults to DEFAULT_POPUP_MAX_WIDTH.
closeButton booleanShow a close button. Read once. Defaults to true.
ref bindablePopup | nullThe MapLibre popup instance. Defaults to null.

Events

NameTypeDescription
onopen () => voidCalled when the popup opens.
onclose () => voidCalled when the popup closes.

Snippets

NameTypeDescription
children Snippet

Other attributes go to the underlying <div>.

Control Container Popup

On This Page

UsagePartsDraggingMoving markersAPI Reference

Handcrafted by Oggy. The source code is on GitHub.