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

Basemap Control

Previous Next

A menu to switch the basemap and turn overlay layers on and off.

<script lang="ts">
	import {
		Map,
		MapBasemapControl,
		MapControlContainer,
		MapPolygon,
		MapPolyline,
		type MapOverlay
	} from '$lib/components/ui/map/index.js';

	const OVERLAYS: MapOverlay[] = [
		{ id: 'park', label: 'Vondelpark' },
		{ id: 'route', label: 'Canal walk' }
	];

	const PARK: [number, number][] = [
		[4.8565, 52.3555],
		[4.8705, 52.3615],
		[4.8795, 52.3618],
		[4.8775, 52.3595],
		[4.8655, 52.3555],
		[4.858, 52.3535]
	];

	const ROUTE: [number, number][] = [
		[4.8838, 52.3667],
		[4.8862, 52.3712],
		[4.8858, 52.3758],
		[4.8895, 52.3795],
		[4.8952, 52.3792]
	];

	let basemap = $state('default');
	let activeOverlays = $state(['park']);
</script>

<Map class="size-full" viewport={{ center: [4.876, 52.366], zoom: 13, bearing: 0, pitch: 0 }}>
	{#if activeOverlays.includes('park')}
		<MapPolygon positions={PARK} fillOpacity={0.2} />
	{/if}
	{#if activeOverlays.includes('route')}
		<MapPolyline positions={ROUTE} width={4} dashArray={[1, 1.5]} />
	{/if}
	<MapControlContainer position="top-right">
		<MapBasemapControl overlays={OVERLAYS} bind:value={basemap} bind:activeOverlays />
	</MapControlContainer>
</Map>

The basemap control is a layers button that opens a menu. The top group picks one basemap. The bottom group, shown when you pass overlays, has a checkbox for each overlay. The menu stays open while you tick overlays, so you can change several at once.

Switching the basemap keeps everything you drew on the map: markers, shapes and layers stay in place.

Usage

Place the control inside a Control Container.

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

<Map class="h-96">
	<MapControlContainer position="top-right">
		<MapBasemapControl />
	</MapControlContainer>
</Map>

Basemaps

By default the menu offers OPENFREEMAP_BASEMAPS: Default, Streets, Bright and Blank. None of them needs an API key. Default follows the light and dark theme.

Pass basemaps to offer your own list. Each entry is a MapBasemap:

  • id: a unique key, used for value.
  • label: the text in the menu.
  • style: a style URL, a style object, or { light, dark } with one of each per theme.
<script lang="ts">
	import {
		MapBasemapControl,
		OPENFREEMAP_BASEMAPS,
		type MapBasemap
	} from '$lib/components/ui/map/index.js';

	const basemaps: MapBasemap[] = [
		...OPENFREEMAP_BASEMAPS,
		{ id: 'terrain', label: 'Terrain', style: 'https://example.com/styles/terrain.json' }
	];

	let basemap = $state('streets');
</script>

<MapBasemapControl {basemaps} bind:value={basemap} />

Bind value to read or set the chosen basemap, or use onvaluechange to hear about picks. An id that is not in the list falls back to the first basemap.

While the control is mounted, it decides the map style and overrides the style prop on Map. When it is removed, the map goes back to its own style. If your style loads from a host other than OpenFreeMap, add that host to your Content Security Policy.

Overlays

An overlay is any layer the user can switch on and off. Pass overlays as a list of MapOverlay objects, each with an id and a label, and bind activeOverlays to the ids that are on.

The control only keeps the list of ids. You render each overlay yourself and show it while its id is active:

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

	const overlays: MapOverlay[] = [{ id: 'park', label: 'Park' }];
	let activeOverlays = $state(['park']);
</script>

<Map class="h-96">
	{#if activeOverlays.includes('park')}
		<MapPolygon positions={park} />
	{/if}
	<MapControlContainer position="top-right">
		<MapBasemapControl {overlays} bind:activeOverlays />
	</MapControlContainer>
</Map>

Fullscreen

The menu opens inside the map element, not at the end of the page. It still shows when the map is in fullscreen with the Fullscreen Control.

API Reference

MapBasemapControl

Props

NameTypeDescription
basemaps MapBasemap[]Basemaps to pick from. While this control is mounted it decides the map style. Defaults to OPENFREEMAP_BASEMAPS.
value bindablestringId of the chosen basemap. Defaults to the first one. Defaults to basemaps[0]?.id ?? ''.
overlays MapOverlay[]Layers to switch on and off. Defaults to [].
activeOverlays bindablestring[]Ids of the overlays switched on. Defaults to [].
label stringName of the menu button. Defaults to 'Map layers'.
class string

Events

NameTypeDescription
onvaluechange (value: string) => voidCalled with the id of the basemap the user picks.
Locate Control Search Control

On This Page

UsageBasemapsOverlaysFullscreenAPI Reference

Handcrafted by Oggy. The source code is on GitHub.