JSFiddle - React, Tailwind, and code Playground

by PantelisChin

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.js"></script>
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.css"
      rel="stylesheet"
    />
    <link rel="stylesheet" type="text/css" href="styles.css">
  </head>
  <body>
    <div id="map"></div>
    <div id="custom-compass" class="compass-control">
      <div class="compass-button" id="compass-button">
        <div class="compass-icon">
          <div class="compass-needle"></div>
          <div class="compass-circle"></div>
        </div>
      </div>
    </div>
    <script src="your-script.js"></script>
  </body>
</html>

CSS

#custom-compass {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 1;
}

.compass-control {
    background: #fff;
    border: 1px solid #ccc;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
}

.compass-icon {
    width: 20px;
    height: 20px;
    position: relative;
}

.compass-circle {
    width: 100%;
    height: 100%;
    background-color: #4285F4;
    border-radius: 50%;
    opacity: 0.7;
}

.compass-needle {
    width: 4px;
    height: 50px;
    background-color: #fff;
    border-radius: 2px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
}

JavaScript

mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';

// Initialize the map
const map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v11',
    center: [0, 0],
    zoom: 10,
});

// Add a custom compass control
map.addControl(new mapboxgl.NavigationControl({ showCompass: false }));

// Add a click event to toggle the map rotation
document.getElementById('custom-compass').addEventListener('click', () => {
    map.easeTo({ bearing: 0 });
});