Change the style of your map

Estimated reading time: 1 minute

Switch between the default Jawg styles for your map using this code sample that uses the MapLibre GL JS library.

View on GitHub

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8" />
    <link href="https://unpkg.com/[email protected]/dist/maplibre-gl.css" rel="stylesheet" />
    <script src="https://unpkg.com/[email protected]/dist/maplibre-gl.js"></script>
    <style>
      html,
      body {
        margin: 0;
        padding: 0;
        height: 100%;
      }
      #map {
        min-height: 500px;
        height: 100%;
        width: 100%;
      }
      select {
        z-index: 1;
        position: absolute;
        top: 5px;
        left: 5px;
        padding: 5px;
        background: white;
      }
    </style>
  </head>
  <body>
    <div id="map">
      <select id="styles" class="dropdownSelect">
        <option value="jawg-streets">jawg-streets</option>
        <option value="jawg-lagoon">jawg-lagoon</option>
        <option value="jawg-sunny">jawg-sunny</option>
        <option value="jawg-terrain">jawg-terrain</option>
        <option value="jawg-dark">jawg-dark</option>
        <option value="jawg-light">jawg-light</option>
      </select>
    </div>
    <script>
      // Don't forget to replace <YOUR_ACCESS_TOKEN> by your own access token
      const accessToken = "<YOUR_ACCESS_TOKEN>";

      const styleList = document.getElementById("styles");
      const options = styleList.getElementsByTagName("option");

      const map = new maplibregl.Map({
        container: "map",
        style: `https://api.jawg.io/styles/${options[0].value}.json?access-token=${accessToken}`,
        zoom: 13,
        center: [5.3704085, 43.2963844],
      }).addControl(new maplibregl.NavigationControl(), "top-right");
      // This plugin is used for right to left languages
      maplibregl.setRTLTextPlugin("https://unpkg.com/@mapbox/[email protected]/mapbox-gl-rtl-text.min.js");

      map.on("load", () => {
        document.getElementById("styles").addEventListener("change", function (event) {
          const styleId = event.target.value;
          map.setStyle(`https://api.jawg.io/styles/${styleId}.json?access-token=${accessToken}`);
        });
      });
    </script>
  </body>
</html>