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

GeoJSON

Previous Next

Draw any GeoJSON polygons, lines and points, with hover and click.

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

	function box(name: string, west: number, south: number, east: number, north: number) {
		return {
			type: 'Feature' as const,
			properties: { name },
			geometry: {
				type: 'Polygon' as const,
				coordinates: [
					[
						[west, south],
						[east, south],
						[east, north],
						[west, north],
						[west, south]
					]
				]
			}
		};
	}

	const ZONES: FeatureCollection<Polygon, { name: string }> = {
		type: 'FeatureCollection',
		features: [
			box('Delivery zone A', -0.16, 51.5, -0.12, 51.525),
			box('Delivery zone B', -0.115, 51.5, -0.075, 51.525),
			box('Delivery zone C', -0.16, 51.475, -0.12, 51.497),
			box('Delivery zone D', -0.115, 51.475, -0.075, 51.497)
		]
	};

	let hovered = $state<string | null>(null);
</script>

<div class="relative size-full">
	<Map class="size-full" viewport={{ center: [-0.1175, 51.5], zoom: 11.5, bearing: 0, pitch: 0 }}>
		<MapGeoJSON
			data={ZONES}
			promoteId="name"
			onhover={(feature) => (hovered = feature ? String(feature.properties.name) : null)}
		/>
	</Map>
	<p
		class="absolute top-3 left-3 rounded-md bg-background/90 px-2 py-1 text-xs shadow"
		aria-live="polite"
	>
		{hovered ?? 'Hover a zone'}
	</p>
</div>

MapGeoJSON draws a GeoJSON Feature or FeatureCollection. Polygons get a fill and an outline, lines get a line, and points get a circle. The hovered feature is highlighted.

Usage

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

	let { zones }: { zones: FeatureCollection } = $props();
</script>

<Map class="h-96">
	<MapGeoJSON data={zones} onclick={(feature) => console.log(feature.properties)} />
</Map>

Data

data takes a GeoJSON object or a URL. With a URL, MapLibre fetches the file itself. If your site has a Content Security Policy, its connect-src must allow that host.

Change data and the source updates in place. The layers are not rebuilt.

Hover and click

onhover gets the feature under the pointer, and null when the pointer leaves. onclick gets the clicked feature and the map event.

The hovered feature is drawn with hoverFillOpacity, hoverLineWidth and hoverCircleRadius. Hover tracks features by id. By default ids are positions in the data, so the hover clears when data changes. Set promoteId to a property name, such as "name", to use that property as the id instead.

Look

Fills, lines and points use the theme’s --primary colour by default and follow the theme. Set fillColor, lineColor and circleColor to change them. The layers survive a basemap or theme swap.

Layer ids

The source id comes from id, or a unique id from $props.id(). The layers are <id>-fill, <id>-line and <id>-circle. Polygon outlines are drawn by the line layer. Pass beforeId to draw below another layer; changing it moves the layers.

Keyboard

When the layer has onclick or onhover, its features are also offered as a list for keyboard and screen reader users, one tab stop on the map. Moving through the list highlights each shape, calls onhover and pans it into view. Enter calls onclick with the feature and no mouse event. Features are named by their name property; pass featureLabel and listLabel to name them another way, or keyboard={false} to leave the list out. Data loaded from a URL is not listed.

API Reference

MapGeoJSON

Props

NameTypeDescription
data required FeatureCollection | Feature | stringGeoJSON to draw, or a URL to load it from. Changing it updates the source in place.
id stringSource id; layers are `<id>-fill`, `<id>-line` and `<id>-circle`. Read once.
promoteId stringUse this feature property as the feature id for hover state. Read once.
fillColor stringPolygon fill. Defaults to the theme's `--primary`.
fillOpacity numberPolygon fill opacity, 0 to 1. Defaults to 0.2.
hoverFillOpacity numberFill opacity of the hovered feature. Defaults to 0.4.
lineColor stringLines and polygon outlines. Defaults to the theme's `--primary`.
lineWidth numberLine width in pixels. Defaults to 2.
hoverLineWidth numberLine width of the hovered feature. Defaults to 3.
circleColor stringPoints. Defaults to the theme's `--primary`.
circleRadius numberPoint radius in pixels. Defaults to 5.
hoverCircleRadius numberPoint radius of the hovered feature. Defaults to 7.
beforeId stringDraw below this layer when it exists; changing it moves the layers.
keyboard booleanOffer the features to keyboard and screen reader users as a list, one tab stop on the map. Only added when `onclick` or `onhover` is set and `data` is not a URL. Defaults to true.
listLabel stringAccessible name of the keyboard list, such as "Delivery zones". Defaults to 'Shapes'.
featureLabel (feature: Feature, index: number) => stringName of a feature in the keyboard list. Defaults to its `name` property.

Events

NameTypeDescription
onclick (feature: MapGeoJSONFeature, event?: MapMouseEvent) => voidCalled with the clicked feature. `event` is left out when the feature was chosen from the keyboard list.
onhover (feature: MapGeoJSONFeature | null) => voidCalled when the pointer moves onto a feature, or keyboard focus reaches it, and with `null` when it leaves.
Point Layer Shapes

On This Page

UsageDataHover and clickLookLayer idsKeyboardAPI Reference

Handcrafted by Oggy. The source code is on GitHub.