Compass rose control
Add a compass rose as a visual indicator of the map’s bearing (rotation) using Custom Controls.
This example demonstrates how to effectively use declarative custom controls to implement a control that follows the state of the map without requiring any JavaScript to create it.
NPM module setup
npm install --save @maptiler/sdk
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';
// Initialize MapTiler SDK
config.apiKey = "YOUR_MAPTILER_API_KEY_HERE";
// Initialize the map
const map = new Map({
container: 'map', // The ID of the HTML element where the map will be rendered
style: MapStyle.DATAVIZ,
zoom: 7,
bearing: 42,
center: [-104.986605, 39.760618],
customControls: true,
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex">
<title>MapTiler Compass Control</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="map"></div>
<img src="./rose.svg" data-maptiler-control data-maptiler-position="top-left" class="compass" />
<script type="module" src="./main.js"></script>
</body>
</html>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
/* Custom control styling */
.compass {
width: 10rem;
margin: 1rem;
transform: rotateZ(calc(var(--maptiler-bearing) * -1deg));
}
Related examples
Custom control programmatically
ExamplesProgrammatically add custom controls for dynamic map logic integration.