A needle that shows the map bearing and turns the map back to north when pressed.
<script lang="ts">
import {
Map,
MapCompassControl,
MapControlContainer,
MapZoomControl
} from '$lib/components/ui/map/index.js';
</script>
<Map class="size-full" viewport={{ center: [-73.9855, 40.758], zoom: 14, bearing: -29, pitch: 45 }}>
<MapControlContainer position="top-right">
<MapZoomControl />
<MapCompassControl />
</MapControlContainer>
</Map>
The compass needle points north and tilts with the camera. Rotate the map with right-click drag or two fingers, and the needle follows. Press the button to turn the map back to north.
Usage
Place the control inside a Control Container, usually under the Zoom Control.
<script lang="ts">
import {
Map,
MapCompassControl,
MapControlContainer,
MapZoomControl
} from '$lib/components/ui/map/index.js';
</script>
<Map class="h-96">
<MapControlContainer position="top-right">
<MapZoomControl />
<MapCompassControl />
</MapControlContainer>
</Map>Keeping the tilt
By default a press also tilts the camera back to flat. Set resetPitch={false} to reset only the bearing. Use onreset to run code after a press, for example to log it.
<MapCompassControl resetPitch={false} onreset={() => console.log('Back to north')} />Reduced motion
The camera turns back over a short animation, and the needle turns smoothly. When the user prefers reduced motion, both change at once.
API Reference
MapCompassControl
Props
| Name | Type | Description |
|---|---|---|
resetPitch | boolean | Also tilt the camera back to flat when pressed. Defaults to true. |
Events
| Name | Type | Description |
|---|---|---|
onreset | () => void | Called after the camera starts turning back to north. |