Change map styles

This tutorial demonstrates the process of altering map styles, allowing users to seamlessly transition between the built-in maps and their customized maps. Explore the selection of pre-designed styles available in MapTiler’s library by referring to the list of built-in styles.

MapTiler provides a wide range of predefined styles, enabling users to customize their maps to suit their specific preferences. Additionally, you have the option to create a completely 100% customized map using MapTiler’s Map Designer tool.

Using the built-in styles and their variants defined in the SDK (maptilersdk.MapStyle) has several advantages such as:

  • If we make an update to a style, you won’t have to modify your codebase. Always use the latest version of styles.
  • They are easier to remember, no need to type along the style URL. No more putting the API key in every URL.
  • Code completion: reducing typos and other common mistakes.

The built-in styles generally define a purpose for which this style is the most relevant: street navigation, outdoor adventure, minimalist dashboard, etc. Then, each style offers a range of variants that contain the same level of information and have the same purpose but use different color schemes like dark, light, etc.

NPM module setup

  1. Install the npm package.

    
     npm install --save @maptiler/sdk
    
  2. Include the CSS file.

    If you have a bundler that can handle CSS, you can import the CSS

    
     import "@maptiler/sdk/dist/maptiler-sdk.css";
    

    or include it with a <link /> in the head of the document via the CDN

    
     <link href='https://cdn.maptiler.com/maptiler-sdk-js/v4.1.0/maptiler-sdk.css' rel='stylesheet' />
    
  3. Include the following code in your JavaScript file (Example: app.js).

    
     import * as maptilersdk from '@maptiler/sdk';
    
     maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
     const map = new maptilersdk.Map({
       container: 'map', // container's id or the HTML element to render the map
       style: maptilersdk.MapStyle.STREETS,
          
       center: [11.5786, 48.0968], // starting position [lng, lat]
       zoom: 10, // starting zoom
          
     });
    
  4. Replace YOUR_MAPTILER_API_KEY_HERE with your own API key. Make sure to secure the key before you publish it.

  5. The next is up to you. You can center your map wherever you desire (modifying the starting position) and set an appropriate zoom level (modifying the starting zoom) to match your users’ needs. Additionally, you can change the map’s look (by updating the source URL); choose from a range of visually appealing map styles from our extensive MapTiler standard maps, or create your own to truly differentiate your application.

  6. Change the map style to a hybrid satellite images.

    
     map.setStyle(MapStyle.STREETS.DARK);
    
  7. Create a selector to change the map style. In this example we will use some of the build-in styles. See the full list of styles. Copy the following code into your HTML file.

    
     <select class="mapstyles-select">
       <optgroup label="Navigation and city exploration">
         <option value="STREETS">STREETS</option>
         <option value="STREETS.DARK" selected>STREETS.DARK</option>
         <option value="STREETS.LIGHT">STREETS.LIGHT</option>
         <option value="STREETS.PASTEL">STREETS.PASTEL</option>
       </optgroup>
       <option value="OUTDOOR">OUTDOOR</option>
       <option value="WINTER">WINTER</option>
       <option value="SATELLITE">SATELLITE</option>
       <option value="HYBRID">HYBRID</option>
       <optgroup label="Data visualization">
         <option value="DATAVIZ">DATAVIZ</option>
         <option value="DATAVIZ.DARK">DATAVIZ.DARK</option>
         <option value="DATAVIZ.LIGHT">DATAVIZ.LIGHT</option>
       </optgroup>
       <option value="YOUR_CUSTOM_MAP_ID_1">Custom map name 1</option>
       <option value="YOUR_CUSTOM_MAP_ID_2">Custom map name 2</option>
     </select>
    
  8. Change the style when the user select one style.

    
     document.querySelector('.mapstyles-select').addEventListener('change', (e) => {
       const style_code = e.target.value.split(".");
       style_code.length === 2 ? 
         map.setStyle(maptilersdk.MapStyle[style_code[0]][style_code[1]]) : 
         map.setStyle(maptilersdk.MapStyle[style_code[0]] || style_code[0]); 
     });
    
  9. Create the layer selector style. Add the selector style to your stylesheet.

    
     .mapstyles-select {
       position: relative;
       top: 5px;
       left: 5px;
       z-index: 1000;
     }
    

Learn more

If you want to switch between built-in styles see this example Built-in map styles.

Check out this tutorial How to display a style switcher control to learn how to create a visual style switcher control.

Built-in map styles

Built-in map styles

Tutorials

Easily create beautiful maps using reliable built-in styles.

How to display a style switcher control

Style switcher

Tutorials

Add a style switcher control to your map.

Set dark mode based on system settings

Set dark mode based on system settings

Examples

Switch map light/dark mode based on system color settings.

How to change the default map labels language

Set map language

Tutorials

This tutorial shows how to change the default map labels language.

Was this helpful?