Skip to content

Error when removing marker on dragend #39

@jcoupey

Description

@jcoupey

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions