Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when removing marker on dragend #39

Open
jcoupey opened this issue Jan 28, 2019 · 0 comments
Open

Error when removing marker on dragend #39

jcoupey opened this issue Jan 28, 2019 · 0 comments

Comments

@jcoupey
Copy link

jcoupey commented Jan 28, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant