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. Install the npm package.

  3. 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

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

  5. Replace YOUR_MAPTILER_API_KEY_HERE with your actual MapTiler API key.

  6. 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.

  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

Related examples

An extension of MapLibre GL JS