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
-
Copy the following code, paste it into your favorite text editor, and save it as a
.html
file. -
Install the npm package.
-
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 -
Include the following code in your JavaScript file (Example: app.js).
-
Replace
YOUR_MAPTILER_API_KEY_HERE
with your actual MapTiler API key. -
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 thestarting zoom
) to match your users’ needs. Additionally, you can change the map’s look (by updating thesource 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.
-
Check whether or not the terrain is enabled.
-
Toggle the map terrain.
-
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. -
Add an event handler for map terrain events. You will add the code to tilt/untilt the map in this handler.
-
Toggle the map pitch.
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:
Check out the tutorial Display a 3D terrain map
Consult the SDK JS Reference