[GIS] Removing individual markers from map

eventsleaflet

I have a map with several moving markers on it and have been trying to remove each individual one when their respective animation ends. I am using Leaflet and Leaflet's plugin Leaflet.MovingMarker. I tried using methods isEnded() and end without much sucess.

With the latter method (end), the following code removes only but one marker (full code here):

marker.on('end', function() {
  map.removeLayer(this);
  });

With a click event instead of an end event, each marker is made easily removable, but then this is not what I am looking for:

marker.on('click', function() {
  map.removeLayer(this);
  });

With the method isEnded(), have not had any luck at all either (full code here):

var ended = marker.isEnded(); 

  if (ended = true) {
  console.log("this marker ended!");
  map.removeLayer(this);
  }

I wonder what I am missing here?

Best Answer

It is possible to use the 'end' event by adding it to the marker's declaration:

 var marker = L.Marker.movingMarker(coordinates, [6000]).on('end', 
    function(e) {
        console.log('end move');          
    });

I couldn't get the layer to be removed, here are some solutions for how to remove individual markers.