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

Route

Previous Next

A route line that shows how far along a trip is, with dots at the start and the end and a dot or marker at the current position.

<script lang="ts">
	import Navigation2Icon from '@lucide/svelte/icons/navigation-2';
	import { Label } from '$lib/components/ui/label/index.js';
	import {
		Map,
		MapMarker,
		MapMarkerContent,
		MapRoute,
		lineLength,
		type LngLatTuple
	} from '$lib/components/ui/map/index.js';

	const ROUTE: LngLatTuple[] = [
		[4.8897, 52.3731],
		[4.8862, 52.3702],
		[4.8834, 52.3661],
		[4.8866, 52.3628],
		[4.8917, 52.3594],
		[4.8995, 52.3581],
		[4.9071, 52.3603],
		[4.9133, 52.3584]
	];
	const METRES_PER_KM = 1000;
	const total = lineLength(ROUTE) / METRES_PER_KM;

	const id = $props.id();
	let percent = $state(40);
	const progress = $derived(percent / 100);
</script>

<div class="relative size-full">
	<Map class="size-full" viewport={{ center: [4.899, 52.3655], zoom: 13.6, bearing: 0, pitch: 0 }}>
		<MapRoute positions={ROUTE} {progress} restDashArray={[1, 2]}>
			{#snippet marker({ lngLat, heading })}
				<MapMarker {lngLat} aria-label="Courier" rotation={heading} rotationAlignment="map">
					<MapMarkerContent>
						<span
							class="grid size-8 place-items-center rounded-full bg-primary text-primary-foreground shadow-lg ring-4 ring-primary/25"
						>
							<Navigation2Icon class="size-4 fill-current" />
						</span>
					</MapMarkerContent>
				</MapMarker>
			{/snippet}
		</MapRoute>
	</Map>
	<div
		class="absolute top-3 left-3 flex w-56 flex-col gap-2 rounded-md bg-background/90 p-3 text-sm shadow"
	>
		<Label for={`${id}-progress`}>Courier progress</Label>
		<input
			id={`${id}-progress`}
			type="range"
			min="0"
			max="100"
			bind:value={percent}
			class="accent-primary"
		/>
		<p class="font-mono text-xs text-muted-foreground" aria-live="polite">
			{(total * progress).toFixed(1)} of {total.toFixed(1)} km
		</p>
	</div>
</div>

MapRoute draws a line through positions in travel order. Set progress from 0 to 1 and the line splits in two: the travelled part in the theme’s primary colour, the part still to go in the muted colour. The split is measured along the ground, so 0.5 means half the distance, not half the points.

A thin outline in the background colour keeps the line readable on any basemap.

Usage

<script lang="ts">
	import { Map, MapRoute } from '$lib/components/ui/map/index.js';

	const route: [number, number][] = [
		[4.8897, 52.3731],
		[4.8834, 52.3661],
		[4.8995, 52.3581],
		[4.9133, 52.3584]
	];
</script>

<Map class="h-96">
	<MapRoute positions={route} progress={0.4} />
</Map>

Progress

progress defaults to 1, which draws the whole route as travelled. Change it as the trip goes on and the line updates in place. Values below 0 or above 1 are clamped.

To show the part still to go as a dashed line, pass restDashArray, in line widths. [1, 2] gives short dashes with wider gaps.

Stops and head

With showStops, the route gets a dot at its first and last position. The start dot takes the travelled colour. The end dot stays muted until progress reaches 1.

With showHead, a dot marks where the travelled part ends. It only shows while the trip is under way, so not at 0 or 1. Both are on by default.

Marker

For a richer marker, such as a vehicle icon, pass a marker snippet. The route renders it at the point progress has reached, and it replaces the head dot. Unlike the head dot it also shows at 0 and 1, so a vehicle stays on the map before it sets off and after it arrives.

The snippet gets lngLat and heading, the way the route runs at that point in degrees clockwise from north. Give a Marker rotation={heading} and rotationAlignment="map" and an arrow keeps pointing along the line, even when the map is rotated:

<MapRoute positions={route} {progress}>
	{#snippet marker({ lngLat, heading })}
		<MapMarker {lngLat} aria-label="Courier" rotation={heading} rotationAlignment="map">
			<MapMarkerContent>
				<Navigation2Icon class="size-5" />
			</MapMarkerContent>
		</MapMarker>
	{/snippet}
</MapRoute>

Leave out rotation for an icon that should stay upright, like a bike or an avatar. Outside a route, splitLine and lineHeadingAt from the utilities give you the same point and heading.

Layers

The line is one source, <id>, with three layers: <id>-casing, <id>-rest and <id>-done. The stop and head dots are a second, small source, <id>-points, drawn by <id>-stops. The travelled part is a gradient on the full line, so animating progress changes a paint value and moves one dot; the line is only sent to MapLibre’s worker again when positions change. Pass id if you need to refer to them, for example as a beforeId for another layer. onclick fires when the line itself is clicked, not the dots.

API Reference

MapRoute

Props

NameTypeDescription
positions required LngLatTuple[]Points along the route as `[longitude, latitude]`, in travel order.
id stringSource id; layers are `<id>-casing`, `<id>-rest`, `<id>-done` and `<id>-stops` (on source `<id>-points`). Read once.
progress numberShare travelled, 0 to 1. The travelled part uses `color`, the rest `restColor`. Defaults to 1.
color stringTravelled part. Defaults to the theme's `--primary`.
restColor stringPart still to go. Defaults to the theme's `--muted-foreground`.
restDashArray number[]Dash pattern of the part still to go, in line widths.
width numberLine width in pixels. Defaults to 4.
casingWidth numberOutline around the line, in pixels on each side. 0 turns it off. Defaults to 2.
showStops booleanDraw dots at the start and the end. Defaults to true.
showHead booleanDraw a dot where the travelled part ends, while the trip is under way. Defaults to true.
beforeId stringDraw below this layer when it exists; changing it moves the layers.

Events

NameTypeDescription
onclick (event: MapMouseEvent) => voidCalled when the route line is clicked.

Snippets

NameTypeDescription
marker Snippet<[MapRouteHead]>Content that follows the trip, usually a `MapMarker`. It gets the point at `progress` and the route's heading there; it replaces the head dot. Give the marker `rotation={heading}` and `rotationAlignment="map"` to turn it with the route.
Shapes Arc

On This Page

UsageProgressStops and headMarkerLayersAPI Reference

Handcrafted by Oggy. The source code is on GitHub.