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 to learn more about it.

Click on the map to enable/disable the map terrain

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

    
     map.on('click', async function() {
        
     });
    
  7. Check whether or not the terrain is enabled.

    
     map.on('click', async function() {
       if (map.hasTerrain()) {
            
       } else {
            
       }
     });
    
  8. Toggle the map terrain.

    
     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.

    
     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.

    
     map.on('terrain', async function() {
        
     });
    
  11. Toggle the map pitch.

    
     map.on('terrain', async function() {
       if (map.hasTerrain()) {
         map.easeTo({ pitch: map.getMaxPitch(), duration: 2000 });
       } else {
         map.easeTo({ pitch: 0, duration: 2000 });
       }
     });
    

Learn more

To change the animate the transition when changing the map pitch check the Animation Options reference. By default is a bezier-curve, but you can define you own function. For example:


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

Consult the SDK JS Reference

Display a 3D terrain map

3D terrain map

Tutorials

Create and display 3D terrain maps on web pages.

Getting started with AR maps: Display an AR control on your maps

Start with AR maps

Examples

Add an AR control button to map 3D viewports.

Add hillshading

Add hillshading

Examples

Add raster hillshading effect to a MapTiler map.

Was this helpful?