Enable/disable map terrain

This tutorial demonstrates how to enable/disable the 3D terrain map programmatically 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

Click on the map to enable/disable the map terrain

  1. Copy the following code, paste it into your favorite text editor, and save it as a .html file.

  2. Replace YOUR_MAPTILER_API_KEY_HERE with your actual MapTiler API key.

  3. The next it’s up to you. You can start the map in a different place by modifying the starting position and starting zoom, and you can change the look of the map to any of our styles, or yours, by updating the style. See what’s available here.

  4. Install the npm package.

  5. Include the CSS file.

    If you have a bundler that can handle CSS, you can import the CSS or include it with a <link> in the head of the document via the CDN

  6. Include the following code in your JavaScript file (Example: app.js).

  7. Capture when the user clicks on the map. Add event handler for map click event. You will add code to enable/diable the terrain in this handler.

  8. Check whether or not the terrian is enabled.

  9. Toggle the map terrain.

  10. 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 volumen.

  11. Add event handler for map terrain event. You will add code to tilt/untilt the map in this handler.

  12. Tilt animation. To animate the transition when changing the map pitch, let’s create a function called changePitch.

  13. Toggle the map pitch.

Learn more

Check out the tutorial Display a 3D terrain map

Consult the SDK JS Reference

An extension of MapLibre GL JS