I want to create a Mapbox IControl
object, which zooms the map in when a button in the control is clicked.
This is my custom IControl class:
class customControl {
_map;
_container;
_myButton;
constructor() {
this._myButton = document.createElement('button');
this._myButton.className = 'mapboxgl-ctrl-zoom-in';
this._myButton.type = 'button';
this._myButton.title = 'Test';
this._myButton.onclick = this._myAction;
this._container = document.createElement('div');
this._container.className = 'mapboxgl-ctrl-group mapboxgl-ctrl';
this._container.appendChild(this._myButton);
}
onAdd(map) {
this._map = map;
return this._container;
}
onRemove() {
this._container.parentNode.removeChild(this._container);
this._map = undefined;
}
_myAction() {
const map = this._map;
console.log(this._map);
map.zoomIn();
}
}
I add this control to my map using the following code:
let myControl = new customControl();
map.addControl(myControl, 'bottom-left');
When the button is clicked, I however get an Cannot read properties of undefined (reading 'zoomIn')
error message, since this._map
is obviously undefined. When I remove the const map = this._map;
line from the _myAction()
function, the zoom in works as expected, but the global map object is used.
Why is this._map
undefined in _myAction()
? Is there a way to make _myAction()
aware of the "scope" the button is clicked, so I can access properties of my custom IControl when clicked?