-
Notifications
You must be signed in to change notification settings - Fork 41
Description
Hi, and first of all thanks for making this plug-in available!
I'm using Leaflet.Path.Drag v1.1.0 with leaflet 1.4 and I want to remove a marker on dragend. Here is a minimal example of what I'm trying to do:
var myLatLng = L.latLng(48, 5);
map.setView(myLatLng, 11);
var myMarker = L.circleMarker(myLatLng, {draggable: true}).addTo(map);
myMarker.on('dragend', function(e) {
map.removeLayer(myMarker);
});What happens is that the marker is removed all right, but I'm getting an Uncaught TypeError: Cannot read property 'dragging' of null error that comes from the end of the _onDragEnd function here:
Leaflet.Path.Drag/dist/L.Path.Drag-src.js
Line 270 in df9c39b
| this._path._map.dragging.enable(); |
It looks like removing the marker from the map invalidates the map reference stored in this._path._map? The workaround I've come up with is to store a direct reference to the map, like this:
--- a/include/leaflet-path-drag-1.1.0/L.Path.Drag-src.js
+++ b/include/leaflet-path-drag-1.1.0/L.Path.Drag-src.js
@@ -160,6 +160,8 @@ L.Handler.PathDrag = L.Handler.extend( /** @lends L.Path.Drag.prototype */ {
.on(document, MOVE[eventType], this._onDrag, this)
.on(document, END[eventType], this._onDragEnd, this);
+ this._map = this._path._map;
+
if (this._path._map.dragging.enabled()) {
// I guess it's required because mousdown gets simulated with a delay
//this._path._map.dragging._draggable._onUp(evt);
@@ -267,7 +269,7 @@ L.Handler.PathDrag = L.Handler.extend( /** @lends L.Path.Drag.prototype */ {
if (this._mapDraggingWasEnabled) {
if (moved) L.DomEvent.fakeStop({ type: 'click' });
- this._path._map.dragging.enable();
+ this._map.dragging.enable();
}
},It works but maybe there is a better way? Also let me know if you'd be interested in having a PR for this.