Map Compass
This example demonstrates how to render a compass icon that displays the heading rotation of an interactive map. The compass indicates the direction where 0 degrees is north, 90 degrees is east, 180 degrees is south, and 270 degrees is west. You will also learn how to rotate the map back to its default north-up orientation.
Live Demo
Code Implementation
SDK Initialization, Compass Image, and Rotation Logic
Key Features
Map Angle Synchronization
The example registers a callback to track the map's rotation angle:
- Angle Tracking: The
registerMapAngleUpdateCallbackmonitors the map's heading - Real-time Updates: Compass rotates smoothly as the map orientation changes
- Visual Feedback: The compass icon reflects the current map rotation
Compass Interaction
Clicking the compass icon resets the map orientation:
- North Alignment: Tapping the compass calls
alignNorthUp()to reset the map - Interactive Control: Provides an intuitive way to return to default orientation
- Visual Indicator: The compass always shows which direction is north
How It Works
- Initialize Map: Create the map view and register the angle update callback
- Get Compass Image: Retrieve the compass icon from SDK settings using
EngineMisc.compassEnableSensorON - Create Compass UI: Position the compass in the top-right corner with proper styling
- Track Rotation: Update the compass rotation whenever the map angle changes
- Reset Orientation: Allow users to reset to north-up by clicking the compass
Implementation Details
Compass Image
The compass image is obtained from the SDK's built-in resources:
- Uses
SdkSettings.getImageById()to get the compass icon - Converts image data to base64 if needed for browser display
- Supports both string URLs and binary image data
Rotation Calculation
The compass rotation is calculated using:
- Map angle in degrees (0° = North, 90° = East, 180° = South, 270° = West)
- Negative rotation applied to compass to show north direction
- Smooth CSS transitions for visual appeal
User Interaction
The compass provides interactive functionality:
- Click handler attached to compass element
- Calls
map.alignNorthUp()to reset map orientation - Visual feedback with cursor pointer and hover effects
Next Steps
- Map Perspective - Control the camera viewing angle
- Follow Position - Track user position
- Map Gestures - Handle touch and mouse interactions