<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
MapCircletakes acenterand aradiusin metres on the ground, so it grows and shrinks with the zoom.stepssets how many points make up the edge.MapRectangletakesboundsas[[west, south], [east, north]].MapPolygontakespositions. You do not need to repeat the first point at the end; the ring is closed for you.holescuts rings out of it.MapPolylinetakespositionsand 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
| Name | Type | Description |
|---|---|---|
id | string | Source id; layers are `<id>-fill` and `<id>-line`. Read once. Defaults to a unique id. |
color | string | Outline colour. Defaults to the theme's `--primary`. |
width | number | Outline width in pixels. |
opacity | number | Outline opacity, 0 to 1. |
dashArray | number[] | Dash pattern in line widths, such as `[2, 2]`. |
beforeId | string | Draw below this layer when it exists; changing it moves the layers. |
fillColor | string | Fill colour. Defaults to `color`. |
fillOpacity | number | Fill opacity, 0 to 1. |
center required | LngLatTuple | `[longitude, latitude]` of the middle. |
radius required | number | Radius in metres on the ground, so the circle scales with the map. |
steps | number | Points along the edge. More is smoother. |
Events
| Name | Type | Description |
|---|---|---|
onclick | (event: MapMouseEvent) => void | Called when the shape is clicked. |
MapPolygon
Props
| Name | Type | Description |
|---|---|---|
id | string | Source id; layers are `<id>-fill` and `<id>-line`. Read once. Defaults to a unique id. |
color | string | Outline colour. Defaults to the theme's `--primary`. |
width | number | Outline width in pixels. |
opacity | number | Outline opacity, 0 to 1. |
dashArray | number[] | Dash pattern in line widths, such as `[2, 2]`. |
beforeId | string | Draw below this layer when it exists; changing it moves the layers. |
fillColor | string | Fill colour. Defaults to `color`. |
fillOpacity | number | Fill 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
| Name | Type | Description |
|---|---|---|
onclick | (event: MapMouseEvent) => void | Called when the shape is clicked. |
MapPolyline
Props
| Name | Type | Description |
|---|---|---|
id | string | Source id; layers are `<id>-fill` and `<id>-line`. Read once. Defaults to a unique id. |
color | string | Outline colour. Defaults to the theme's `--primary`. |
width | number | Outline width in pixels. |
opacity | number | Outline opacity, 0 to 1. |
dashArray | number[] | Dash pattern in line widths, such as `[2, 2]`. |
beforeId | string | Draw below this layer when it exists; changing it moves the layers. |
positions required | LngLatTuple[] | Points along the line as `[longitude, latitude]`. |
Events
| Name | Type | Description |
|---|---|---|
onclick | (event: MapMouseEvent) => void | Called when the shape is clicked. |
MapRectangle
Props
| Name | Type | Description |
|---|---|---|
id | string | Source id; layers are `<id>-fill` and `<id>-line`. Read once. Defaults to a unique id. |
color | string | Outline colour. Defaults to the theme's `--primary`. |
width | number | Outline width in pixels. |
opacity | number | Outline opacity, 0 to 1. |
dashArray | number[] | Dash pattern in line widths, such as `[2, 2]`. |
beforeId | string | Draw below this layer when it exists; changing it moves the layers. |
fillColor | string | Fill colour. Defaults to `color`. |
fillOpacity | number | Fill opacity, 0 to 1. |
bounds required | LngLatBoundsTuple | `[[west, south], [east, north]]`. |
Events
| Name | Type | Description |
|---|---|---|
onclick | (event: MapMouseEvent) => void | Called when the shape is clicked. |