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

Compass Control

Previous Next

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

NameTypeDescription
resetPitch booleanAlso tilt the camera back to flat when pressed. Defaults to true.

Events

NameTypeDescription
onreset () => voidCalled after the camera starts turning back to north.
Zoom Control Scale Control

On This Page

UsageKeeping the tiltReduced motionAPI Reference

Handcrafted by Oggy. The source code is on GitHub.