Would it be possible to add toggle button for 3d terrain, similar to GeolocateControl or FullscreenControl that is built in to maplibre? I feel like this would make it easier to optionally use terrain.
I have done this on my own site with this code and css. I used a terrain icon from svgrepo which looks good for show 3d terrain. For hide 3d terrain I removed the montains in the icon, which works but is kind of ugly (i had trouble thinking of something better)
/* Code to add a custom button. Idea from Stack Overflow https://stackoverflow.com/a/51683226 */
class MaplibreGLButtonControl {
constructor({
className = "",
id = "",
title = "",
eventHandler = evtHndlr
}) {
this._className = className;
this._id = id;
this._title = title;
this._eventHandler = eventHandler;
}
onAdd(map) {
this._btn = document.createElement("button");
this._btn.id = this._id;
this._btn.className = "maplibregl-ctrl-icon" + " " + this._className;
this._btn.type = "button";
this._btn.title = this._title;
this._btn.onclick = this._eventHandler;
this._container = document.createElement("div");
this._container.className = "maplibregl-ctrl-group maplibregl-ctrl";
this._container.appendChild(this._btn);
return this._container;
}
onRemove() {
this._container.parentNode.removeChild(this._container);
this._map = undefined;
}
}
/* Toggle Terrain Function */
function terrain_toggle(event) {
var el = document.getElementById(event.target.id);
if ( el.classList.contains('maplibregl-terrain') )
{
map.addTerrain("terrain");
el.title = "Hide 3d Terrain"
} else {
map.removeTerrain();
el.title = "Show 3d Terrain"
}
el.classList.toggle('maplibregl-terrain');
el.classList.toggle('maplibregl-terrain-hide');
}
/* Toggle Terrain Button */
const terrain_button = new MaplibreGLButtonControl({
className: "maplibregl-terrain",
id: "terrain_button",
title: "Show 3d Terrain",
eventHandler: terrain_toggle
});
map.addControl(terrain_button, "top-right");
/* End Custom Button to Toggle Terrain */
.maplibregl-terrain {
background-image: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='UTF-8'%3F%3E%3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='25px' height='25px' viewBox='-4 -4 25 25' version='1.1'%3E%3Cg id='surface1'%3E%3Cpath style=' stroke:none;fill-rule:nonzero;fill:rgb(0%25,0%25,0%25);fill-opacity:1;' d='M 1.996094 15.234375 L 7.054688 9.722656 L 10.566406 13.234375 L 14.292969 16.957031 L 15.394531 15.855469 L 11.632812 12.089844 L 16.003906 7.410156 L 22.988281 15.234375 L 24.21875 15.234375 L 24.21875 14.265625 L 16.027344 5.089844 L 10.527344 10.984375 L 7.007812 7.464844 L 0.78125 14.246094 L 0.78125 15.234375 Z M 1.996094 15.234375 '/%3E%3Cpath style=' stroke:none;fill-rule:nonzero;fill:rgb(0%25,0%25,0%25);fill-opacity:1;' d='M 0.78125 19.140625 L 24.21875 19.140625 L 24.21875 20.703125 L 0.78125 20.703125 Z M 0.78125 19.140625 '/%3E%3C/g%3E%3C/svg%3E%0A");
}
.maplibregl-terrain-hide {
background-image: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='UTF-8'%3F%3E%3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='25px' height='25px' viewBox='-4 -4 25 25' version='1.1'%3E%3Cg id='surface1'%3E%3Cpath style=' stroke:none;fill-rule:nonzero;fill:rgb(0%25,0%25,0%25);fill-opacity:1;' d='M 0.78125 19.140625 L 24.21875 19.140625 L 24.21875 20.703125 L 0.78125 20.703125 Z M 0.78125 19.140625 '/%3E%3C/g%3E%3C/svg%3E%0A");
}