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

Scale Control

Previous Next

A bar that shows how far a stretch of the map is on the ground.

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

<Map class="size-full" viewport={{ center: [-122.4194, 37.7749], zoom: 12, bearing: 0, pitch: 0 }}>
	<MapControlContainer position="bottom-left">
		<MapScaleControl />
		<MapScaleControl unit="imperial" />
	</MapControlContainer>
</Map>

The scale bar measures the ground distance across the middle of the map and rounds it down to 1, 2, 3 or 5 times a power of ten. It updates as you pan and zoom. The bar is never wider than maxWidth pixels. Screen readers read it as “Map scale” followed by the distance.

Usage

Place the control inside a Control Container. The bottom-left corner keeps it clear of the attribution.

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

<Map class="h-96">
	<MapControlContainer position="bottom-left">
		<MapScaleControl />
	</MapControlContainer>
</Map>

Units

unit="metric" shows kilometres and metres. unit="imperial" shows miles and feet. To show both, stack two controls in the same container, as in the preview above.

<MapScaleControl unit="imperial" maxWidth={120} />

API Reference

MapScaleControl

Props

NameTypeDescription
maxWidth numberLongest the bar may get, in pixels. Defaults to 100.
unit ScaleUnitShow kilometres and metres, or miles and feet. Defaults to 'metric'.

Other attributes go to the underlying <div>.

Compass Control Fullscreen Control

On This Page

UsageUnitsAPI Reference

Handcrafted by Oggy. The source code is on GitHub.