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
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
});
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.
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.
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.
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.
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.
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.
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: