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

Shapes

Previous Next

Circles, polygons, polylines and rectangles from plain coordinates.

<script lang="ts">
	import {
		Map,
		MapCircle,
		MapPolygon,
		MapPolyline,
		MapRectangle,
		type LngLatBoundsTuple,
		type LngLatTuple
	} from '$lib/components/ui/map/index.js';

	const STADIUM: LngLatTuple = [151.2252, -33.8915];
	const PARK: LngLatBoundsTuple = [
		[151.2042, -33.8781],
		[151.2153, -33.8693]
	];
	const MARKET: LngLatTuple[] = [
		[151.1985, -33.8785],
		[151.2035, -33.8775],
		[151.2048, -33.8822],
		[151.1992, -33.8838]
	];
	const WALK: LngLatTuple[] = [
		[151.2035, -33.8775],
		[151.2075, -33.8752],
		[151.2105, -33.8737],
		[151.2153, -33.8693]
	];
</script>

<Map class="size-full" viewport={{ center: [151.212, -33.8805], zoom: 13, bearing: 0, pitch: 0 }}>
	<MapCircle center={STADIUM} radius={500} fillOpacity={0.1} dashArray={[2, 2]} />
	<MapRectangle bounds={PARK} fillOpacity={0.3} />
	<MapPolygon positions={MARKET} fillOpacity={0.5} width={1} />
	<MapPolyline positions={WALK} width={4} dashArray={[0.5, 1.5]} />
</Map>

Shapes draw a single geometry from coordinates, so you do not have to write GeoJSON. Each one is its own source with a line layer, and the filled shapes also have a fill layer. For many features at once, use GeoJSON.

Usage

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

<Map class="h-96">
	<MapCircle center={[151.2252, -33.8915]} radius={500} />
	<MapRectangle
		bounds={[
			[151.2042, -33.8781],
			[151.2153, -33.8693]
		]}
	/>
	<MapPolygon
		positions={[
			[151.1985, -33.8785],
			[151.2035, -33.8775],
			[151.2048, -33.8822]
		]}
	/>
	<MapPolyline
		positions={[
			[151.2035, -33.8775],
			[151.2153, -33.8693]
		]}
	/>
</Map>

All coordinates are [longitude, latitude].

The four shapes

  • MapCircle takes a center and a radius in metres on the ground, so it grows and shrinks with the zoom. steps sets how many points make up the edge.
  • MapRectangle takes bounds as [[west, south], [east, north]].
  • MapPolygon takes positions. You do not need to repeat the first point at the end; the ring is closed for you. holes cuts rings out of it.
  • MapPolyline takes positions and draws a line through them. It has no fill.

Change any of these and the shape updates in place.

Look

The outline uses the theme’s --primary colour by default and follows the theme. Set color, width and opacity for the outline, and fillColor and fillOpacity for the inside. fillColor defaults to color.

dashArray makes a dashed line. Its numbers are in line widths, so [2, 2] draws dashes and gaps twice as long as the line is wide.

Shapes survive a basemap or theme swap.

Click

onclick runs when the user clicks the shape. It gets the map event.

Layer ids

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

API Reference

MapCircle

Props

NameTypeDescription
id stringSource id; layers are `<id>-fill` and `<id>-line`. Read once. Defaults to a unique id.
color stringOutline colour. Defaults to the theme's `--primary`.
width numberOutline width in pixels.
opacity numberOutline opacity, 0 to 1.
dashArray number[]Dash pattern in line widths, such as `[2, 2]`.
beforeId stringDraw below this layer when it exists; changing it moves the layers.
fillColor stringFill colour. Defaults to `color`.
fillOpacity numberFill opacity, 0 to 1.
center required LngLatTuple`[longitude, latitude]` of the middle.
radius required numberRadius in metres on the ground, so the circle scales with the map.
steps numberPoints along the edge. More is smoother.

Events

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

MapPolygon

Props

NameTypeDescription
id stringSource id; layers are `<id>-fill` and `<id>-line`. Read once. Defaults to a unique id.
color stringOutline colour. Defaults to the theme's `--primary`.
width numberOutline width in pixels.
opacity numberOutline opacity, 0 to 1.
dashArray number[]Dash pattern in line widths, such as `[2, 2]`.
beforeId stringDraw below this layer when it exists; changing it moves the layers.
fillColor stringFill colour. Defaults to `color`.
fillOpacity numberFill opacity, 0 to 1.
positions required LngLatTuple[]Corners as `[longitude, latitude]`. The ring is closed for you.
holes LngLatTuple[][]Rings cut out of the polygon, in the same form as `positions`. Defaults to [].

Events

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

MapPolyline

Props

NameTypeDescription
id stringSource id; layers are `<id>-fill` and `<id>-line`. Read once. Defaults to a unique id.
color stringOutline colour. Defaults to the theme's `--primary`.
width numberOutline width in pixels.
opacity numberOutline opacity, 0 to 1.
dashArray number[]Dash pattern in line widths, such as `[2, 2]`.
beforeId stringDraw below this layer when it exists; changing it moves the layers.
positions required LngLatTuple[]Points along the line as `[longitude, latitude]`.

Events

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

MapRectangle

Props

NameTypeDescription
id stringSource id; layers are `<id>-fill` and `<id>-line`. Read once. Defaults to a unique id.
color stringOutline colour. Defaults to the theme's `--primary`.
width numberOutline width in pixels.
opacity numberOutline opacity, 0 to 1.
dashArray number[]Dash pattern in line widths, such as `[2, 2]`.
beforeId stringDraw below this layer when it exists; changing it moves the layers.
fillColor stringFill colour. Defaults to `color`.
fillOpacity numberFill opacity, 0 to 1.
bounds required LngLatBoundsTuple`[[west, south], [east, north]]`.

Events

NameTypeDescription
onclick (event: MapMouseEvent) => voidCalled when the shape is clicked.
GeoJSON Route

On This Page

UsageThe four shapesLookClickLayer idsAPI Reference

Handcrafted by Oggy. The source code is on GitHub.