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

Heatmap

Previous Next

Show where points gather as a smooth density heatmap.

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

	interface Pickup {
		riders: number;
	}

	const SEED = 11;
	const PER_SPOT = 150;
	const MAX_RIDERS = 4;
	/** Busy spots in Madrid and how far pickups scatter around them, in degrees. */
	const SPOTS: { center: LngLatTuple; spread: number }[] = [
		{ center: [-3.7038, 40.4168], spread: 0.012 },
		{ center: [-3.6883, 40.4531], spread: 0.01 },
		{ center: [-3.6825, 40.4066], spread: 0.008 },
		{ center: [-3.7124, 40.4378], spread: 0.014 }
	];

	/** A small seeded generator, so every load shows the same pickups. */
	function seeded(seed: number): () => number {
		let state = seed;
		return () => {
			state = (state * 16807) % 2147483647;
			return (state - 1) / 2147483646;
		};
	}

	function pickups(): FeatureCollection<Point, Pickup> {
		const random = seeded(SEED);
		const noise = () => random() + random() + random() - 1.5;
		const features = SPOTS.flatMap(({ center, spread }) =>
			Array.from({ length: PER_SPOT }, (): Feature<Point, Pickup> => ({
				type: 'Feature',
				properties: { riders: 1 + Math.floor(random() * MAX_RIDERS) },
				geometry: {
					type: 'Point',
					coordinates: [center[0] + noise() * spread * 2, center[1] + noise() * spread * 1.5]
				}
			}))
		);
		return { type: 'FeatureCollection', features };
	}

	const data = pickups();
	const id = $props.id();
	let radius = $state(20);
</script>

<div class="relative size-full">
	<Map class="size-full" viewport={{ center: [-3.698, 40.43], zoom: 12, bearing: 0, pitch: 0 }}>
		<MapHeatmap {data} weight="riders" {radius} />
	</Map>
	<div
		class="absolute top-3 left-3 flex w-48 flex-col gap-2 rounded-md bg-background/90 p-3 text-sm shadow"
	>
		<Label for={`${id}-radius`}>Radius: {radius} px</Label>
		<input
			id={`${id}-radius`}
			type="range"
			min="5"
			max="40"
			bind:value={radius}
			class="accent-primary"
		/>
	</div>
</div>

MapHeatmap blurs each point into its neighbours and colours the result by density. Where there is no data the layer is see-through, so the basemap shows. Use it when the shape of the data matters more than single points.

Usage

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

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

<Map class="h-96">
	<MapHeatmap data={pickups} />
</Map>

data can also be a URL to a GeoJSON file. MapLibre then loads it for you. If the file is on another origin, add that origin to connect-src in your Content Security Policy.

Weight

By default every point counts once. Set weight to the name of a numeric property and each point counts that many times. A point without the property, or with a value that is not a number, counts once.

<MapHeatmap data={pickups} weight="riders" />

Tuning

  • radius is how far each point spreads, in pixels. Raise it for sparse data, lower it to see detail.
  • intensity multiplies the density. Raise it when the map looks faint, lower it when everything is hot.
  • opacity sets how much of the basemap shows through.

All three can change at any time without rebuilding the layer.

Colours

The ramp runs through the theme’s five chart colours, --chart-1 to --chart-5, from low to high density. It follows light and dark mode. Pass colors to use your own ramp; the first colour is used for the faintest areas.

API Reference

MapHeatmap

Props

NameTypeDescription
data required FeatureCollection<Point> | stringPoints to draw, or a URL to load them from. Changing it updates the source in place.
id stringSource id; the layer is `<id>-heat`. Read once.
weight stringName of a numeric property that weighs each point. Points without it weigh 1.
radius numberBlur radius of each point, in pixels. Defaults to 20.
intensity numberMultiplies the density, for sparse or dense data. Defaults to 1.
opacity numberLayer opacity, 0 to 1. Defaults to 0.8.
colors string[]Colours from low to high density. Defaults to the theme's chart colours.
beforeId stringDraw below this layer when it exists; changing it moves the layers.
Cluster Layer Zoom Control

On This Page

UsageWeightTuningColoursAPI Reference

Handcrafted by Oggy. The source code is on GitHub.