<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
| Name | Type | Description |
|---|---|---|
maxWidth | number | Longest the bar may get, in pixels. Defaults to 100. |
unit | ScaleUnit | Show kilometres and metres, or miles and feet. Defaults to 'metric'. |
Other attributes go to the underlying <div>.