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

Point Layer

Previous Next

Hundreds or thousands of points drawn on the canvas, with an optional popup on click.

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

	interface Sensor {
		name: string;
		pm25: number;
	}

	const CENTER: [number, number] = [-122.4194, 37.7749];
	const COUNT = 400;

	// A golden-angle spiral spreads the points evenly without random numbers.
	const SENSORS: FeatureCollection<Point, Sensor> = {
		type: 'FeatureCollection',
		features: Array.from({ length: COUNT }, (_, index) => {
			const angle = index * 2.39996;
			const distance = 0.0016 * Math.sqrt(index + 1);
			return {
				type: 'Feature',
				properties: { name: `Sensor ${index + 1}`, pm25: 4 + ((index * 7) % 30) },
				geometry: {
					type: 'Point',
					coordinates: [
						CENTER[0] + distance * Math.cos(angle) * 1.3,
						CENTER[1] + distance * Math.sin(angle)
					]
				}
			};
		})
	};
</script>

<Map class="size-full" viewport={{ center: CENTER, zoom: 12, bearing: 0, pitch: 0 }}>
	<MapPointLayer data={SENSORS} radius={4} strokeWidth={1} popupLabel="Air sensor">
		{#snippet popup(feature)}
			<p class="font-medium">{feature.properties.name}</p>
			<p class="text-muted-foreground">PM2.5: {feature.properties.pm25} µg/m³</p>
		{/snippet}
	</MapPointLayer>
</Map>

MapPointLayer draws a GeoJSON FeatureCollection of points as circles on the map canvas. It stays fast with thousands of points.

Use it above about 200 points. For fewer points that need to be focusable buttons with labels and tooltips, use a Marker.

Usage

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

	let { stops }: { stops: FeatureCollection<Point, { name: string }> } = $props();
</script>

<Map class="h-96">
	<MapPointLayer data={stops} popupLabel="Stop">
		{#snippet popup(feature)}
			<p class="font-medium">{feature.properties.name}</p>
		{/snippet}
	</MapPointLayer>
</Map>

Popup

Pass a popup snippet to show a popup when a point is clicked. The snippet gets the clicked feature. There is one popup for the whole layer: it moves to whichever point you click, and a click anywhere else on the map closes it. Escape closes it too.

Set popupLabel to name the popup for screen readers. popupOffset, popupMaxWidth and popupClass change its look.

To handle clicks yourself, use onclick. It gets the feature and its index in data.features.

Updating data

Replace or change data and the points update in place. The layer is not rebuilt.

Look

Points use the theme’s --primary colour with a --background outline, and follow the theme when it changes. Set color, radius, strokeColor and strokeWidth to change them. The layer survives a basemap or theme swap.

Keyboard

Points drawn on the canvas are not elements, so the layer also lists them for keyboard and screen reader users. The list is one tab stop on the map: Tab to it, move with the arrow keys, Page Up and Page Down, Home and End, and press Enter to open a point’s popup. Escape closes the popup and puts focus back on the list. The list stays hidden until it has focus, then shows a few points around the current one in the bottom-left corner. The current point gets a thicker outline and is panned into view. Only those few rows are in the page, so the list stays light with thousands of points, and screen readers still hear the position, such as 12 of 4,000.

Each point is named by its name property. Pass featureLabel to name it another way, and listLabel to name the list, such as “Stores”. If you make the points reachable another way, such as a list beside the map, turn it off with keyboard={false}.

Layer ids

The source id comes from id, or a unique id from $props.id() if you leave it out. The circle layer is <id>-circles. Pass beforeId to draw the points below another layer; changing it moves them.

API Reference

MapPointLayer

Props

NameTypeDescription
data required FeatureCollection<Point, P>Points to draw. Replace or change it to update the map without rebuilding the layer.
id stringSource id; the circle layer is `<id>-circles`. Read once. Defaults to a unique id.
color stringFill colour. Defaults to the theme's `--primary`.
radius numberRadius in pixels. Defaults to 6.
strokeColor stringOutline colour. Defaults to the theme's `--background`.
strokeWidth numberOutline width in pixels. Defaults to 2.
beforeId stringDraw below this layer when it exists; changing it moves the layers.
popupLabel stringAccessible name of the popup. Defaults to the point's `name` property.
popupOffset PopupOffsetPixel offset of the popup from the point. Defaults to 10.
popupMaxWidth stringLargest width of the popup, as CSS. Defaults to DEFAULT_POPUP_MAX_WIDTH.
popupClass stringClasses for the popup content.
keyboard booleanOffer the points to keyboard and screen reader users as a list, one tab stop on the map. Turn it off for very large data sets you make reachable another way. Defaults to true.
listLabel stringAccessible name of the keyboard list, such as "Stores". Defaults to 'Points'.
featureLabel (feature: PointFeature, index: number) => stringName of a point in the keyboard list. Defaults to its `name` property.

Events

NameTypeDescription
onclick (feature: PointFeature, index: number) => voidCalled with the clicked point and its index in `data.features`.

Snippets

NameTypeDescription
popup Snippet<[PointFeature]>Popup shown when a point is clicked.
Popup GeoJSON

On This Page

UsagePopupUpdating dataLookKeyboardLayer idsAPI Reference

Handcrafted by Oggy. The source code is on GitHub.