I can’t overload the following function of the leaflet BoxZoom class using include() method. I need to catch a key press while dragging. If I put a breakpoint in my code no stop happens. It seems the original function is always used.
L.Map.BoxZoom.include({
_onPointerUp: function (evt) {
if (evt.button !== 0) { return; }
this._finish();
if (!this._moved) { return; }
this._clearDeferredResetState();
this._resetStateTimeout = setTimeout(this._resetState.bind(this), 0);
const bounds = new LatLngBounds(
this._map.containerPointToLatLng(this._startPoint),
this._map.containerPointToLatLng(this._point));
if (evt.originalEvent.key == 'x') { //added code
console.log("boxzoom + x");
}
else { // original zoom behavior
this._map
.fitBounds(bounds)
.fire('boxzoomend', { boxZoomBounds: bounds });
}
}
});
See Leaflet code source
So I tried to overwrite it via include
method like in the code above, but also tried to directly overwrite the prototype
function, then after reading that the modification had to be made before initializing the map, I tried different places in my code :
- inside / outside
L.Map.addInitHook
- inside / outside my plugin definition
- inside a
document.onload
… nothing works!
I already used include
method to add new functions (without any problem), but it’s the first time I uses it to change an existing method. The leaflet documentation is clear about this:
L.Class.include()
If a class is already defined, existing properties/methods can be redefined, or new ones can be added by using .include():
I don’t understand what I’m missing.
Any help will be appreciated, thanks in advance.
2