---
title: "Compass rose control | JavaScript maps SDK"
description: "Add a compass rose to indicate map bearing rotation."
---

# 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

```bash
npm install --save @maptiler/sdk
```

#### main.js

```js
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,
});
```

#### index.html

```html
<!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>
```

#### style.css

```css
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));
}
```

### Basic JavaScript setup

```html
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>MapTiler Compass Control</title>
  <!-- Load MapTiler SDK JS and CSS -->
  <script src="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.umd.min.js"></script>
  <link href="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css" rel="stylesheet" />
  <style>
    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));
    }
  </style>
</head>

<body>
  <div id="map"></div>
  <img src="./rose.svg" data-maptiler-control data-maptiler-position="top-left" class="compass" />
  <script>
    // Initialize MapTiler SDK
    maptilersdk.config.apiKey = "YOUR_MAPTILER_API_KEY_HERE";

    // Initialize the map
    const map = new maptilersdk.Map({
      container: 'map', // The ID of the HTML element where the map will be rendered
      style: maptilersdk.MapStyle.DATAVIZ,
      zoom: 7,
      bearing: 42,
      center: [-104.986605, 39.760618],
      customControls: true,
    });
  </script>
</body>

</html>
```

## Related examples

### [Add custom zoom control](https://docs.maptiler.com/sdk-js/examples/custom-control/)

Examples

Add custom zoom level control to your map interface.

### [Custom control declarative](https://docs.maptiler.com/sdk-js/examples/custom-controls-declarative/)

Examples

Add interactive custom controls to maps using declarative HTML.

### [Custom control programmatically](https://docs.maptiler.com/sdk-js/examples/custom-controls-programmatic/)

Examples

Programmatically add custom controls for dynamic map logic integration.

### [Minimap control](https://docs.maptiler.com/sdk-js/examples/control-minimap/)

Tutorials

Display an overview minimap control to aid map navigation.
