---
title: "Enable/disable map terrain | JavaScript maps SDK"
description: "This tutorial demonstrates how to enable/disable the 3D terrain map programmatically using SDK functions."
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs.maptiler.com/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# Enable/disable map terrain

This tutorial demonstrates how to programmatically enabling or disabling the 3D terrain map using SDK functions `map.enableTerrain()` and `map.disableTerrain()`.

The easiest and fastest way to create a map with 3D terrain is through the map options. Check out the tutorial [Display a 3D terrain map](../3d-map/) to learn more about it.

Click on the map to enable/disable the map terrain

## NPM module setup

1.  Install the npm package.
    
    ```bash
    npm install --save @maptiler/sdk
    ```
    
2.  Include the CSS file.
    
    If you have a bundler that can handle CSS, you can `import` the CSS
    
    ```js
    import "@maptiler/sdk/dist/maptiler-sdk.css";
    ```
    
    or include it with a `<link />` in the head of the document via the CDN
    
    ```html
    <link href='https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css' rel='stylesheet' />
    ```
    
3.  Include the following code in your JavaScript file (Example: app.js).
    
    ```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.WINTER,
          
       center: [-106.44336, 39.12108], // starting position [lng, lat]
       zoom: 11, // starting zoom
          
     });
    ```
    
4.  Replace `YOUR_MAPTILER_API_KEY_HERE` with [your own API key](https://cloud.maptiler.com/account/keys/). Make sure to [secure the key](https://docs.maptiler.com/guides/developer/credentials/api-key/#secure-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](https://cloud.maptiler.com/maps/), or create your own to truly differentiate your application.
    
6.  Capture when the user clicks on the map. Add an event handler for the map click event. You will add the code to enable/disable the terrain in this handler.
    
    ```js
    map.on('click', async function() {
        
     });
    ```
    
7.  Check whether or not the terrain is enabled.
    
    ```js
    map.on('click', async function() {
       if (map.hasTerrain()) {
            
       } else {
            
       }
     });
    ```
    
8.  Toggle the map terrain.
    
    ```js
    map.on('click', async function() {
       if (map.hasTerrain()) {
         map.disableTerrain();
       } else {
         map.enableTerrain();
       }
     });
    ```
    
9.  Exaggerate the terrain. The `enableTerrain` function by default enables terrain with an exaggeration factor of 1 (actual values). This function also allows you to activate the terrain with a certain exaggeration factor. In this example, we will apply a factor of 1.5 to accentuate the terrain volume.
    
    ```js
    map.on('click', async function() {
       if (map.hasTerrain()) {
         map.disableTerrain();
       } else {
         map.enableTerrain(1.5);
       }
     });
    ```
    
10.  Add an event handler for map terrain events. You will add the code to tilt/untilt the map in this handler.
     
     ```js
     map.on('terrain', async function() {
         
      });
     ```
     
11.  Toggle the map pitch.
     
     ```js
     map.on('terrain', async function() {
        if (map.hasTerrain()) {
          map.easeTo({ pitch: map.getMaxPitch(), duration: 2000 });
        } else {
          map.easeTo({ pitch: 0, duration: 2000 });
        }
      });
     ```
     

## Basic JavaScript setup

1.  Copy the following code, paste it into your favorite text editor, and save it as a `.html` file.
    
    ```html
    <!DOCTYPE html>
     <html lang="en">
     <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>Enable/disable map terrain</title>
       <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%; }
       </style>
     </head>
     <body>
       <div id="map"></div>
       <script>
           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.WINTER,
                
             center: [-106.44336, 39.12108], // starting position [lng, lat]
             zoom: 11, // starting zoom
                
           });
       </script>
     </body>
     </html>
    ```
    
2.  Replace `YOUR_MAPTILER_API_KEY_HERE` with [your own API key](https://cloud.maptiler.com/account/keys/). Make sure to [secure the key](https://docs.maptiler.com/guides/developer/credentials/api-key/#secure-key) before you publish it.
    
3.  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](https://cloud.maptiler.com/maps/), or create your own to truly differentiate your application.
    
4.  Capture when the user clicks on the map. Add an event handler for the map click event. You will add the code to enable/disable the terrain in this handler.
    
    ```js
    map.on('click', async function() {
        
     });
    ```
    
5.  Check whether or not the terrain is enabled.
    
    ```js
    map.on('click', async function() {
       if (map.hasTerrain()) {
            
       } else {
            
       }
     });
    ```
    
6.  Toggle the map terrain.
    
    ```js
    map.on('click', async function() {
       if (map.hasTerrain()) {
         map.disableTerrain();
       } else {
         map.enableTerrain();
       }
     });
    ```
    
7.  Exaggerate the terrain. The `enableTerrain` function by default enables terrain with an exaggeration factor of 1 (actual values). This function also allows you to activate the terrain with a certain exaggeration factor. In this example, we will apply a factor of 1.5 to accentuate the terrain volume.
    
    ```js
    map.on('click', async function() {
       if (map.hasTerrain()) {
         map.disableTerrain();
       } else {
         map.enableTerrain(1.5);
       }
     });
    ```
    
8.  Add an event handler for map terrain events. You will add the code to tilt/untilt the map in this handler.
    
    ```js
    map.on('terrain', async function() {
        
     });
    ```
    
9.  Toggle the map pitch.
    
    ```js
    map.on('terrain', async function() {
       if (map.hasTerrain()) {
         map.easeTo({ pitch: map.getMaxPitch(), duration: 2000 });
       } else {
         map.easeTo({ pitch: 0, duration: 2000 });
       }
     });
    ```
    

### Complete source code

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Enable/disable map terrain</title>
  <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%; 
    }
  </style>
</head>
<body>
  <div id="map"></div>
  <script>
      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.WINTER,
        center: [-106.44336, 39.12108], // starting position [lng, lat]
        zoom: 11, // starting zoom
      });

      map.on('click', function() {
        if (map.hasTerrain()) {
          map.disableTerrain();
        } else {
          map.enableTerrain(1.5);
        }
      });

      map.on('terrain', function() {
        if (map.hasTerrain()) {
          map.easeTo({ pitch: map.getMaxPitch(), duration: 2000 });
        } else {
          map.easeTo({ pitch: 0, duration: 2000 });
        }
      });
  </script>
</body>
</html>
```

## Learn more

To change the animate the transition when changing the map pitch check the [Animation Options](https://docs.maptiler.com/sdk-js/api/properties/#animationoptions) reference. By default is a `bezier-curve`, but you can define you own function. For example:

```js
map.easeTo({ 
  pitch: 0, 
  duration: 2000, 
  easing: (x) => (x < 0.5 ? 16 * x ** 5 : 1 - (-2 * x + 2) ** 5 / 2) 
});
```

Check out the tutorial [Display a 3D terrain map](../3d-map/)

Consult the [SDK JS Reference](https://docs.maptiler.com/sdk-js/api/map/#map#enableterrain)

## Related examples

### [3D terrain map](https://docs.maptiler.com/sdk-js/examples/3d-map/)

Tutorials

Create and display 3D terrain maps on web pages.

### [Start with AR maps](https://docs.maptiler.com/sdk-js/examples/ar-control/)

Examples

Add an AR control button to map 3D viewports.

### [Add hillshading](https://docs.maptiler.com/sdk-js/examples/hillshade/)

Examples

Add raster hillshading effect to a MapTiler map.
