ewoken / Leaflet.MovingMarker

A Leaflet plug-in to create moving marker
MIT License
342 stars 177 forks source link

[Enhancement] Adding a remove option #12

Open ghost opened 8 years ago

ghost commented 8 years ago

I am attempting to add a custom option to the plugin so that, if truthy, each marker is removed at the end of the respective animation. I think this could be a really useful feature. Also, it is not trivial to remove them individually outside the plugin (please see issue #11).

With this in mind, I've added a destroyedState: 4 line in the statics properties and a remove: false line in the options properties. I've also added the following code blocks:

isDestroyed: function() {
    return this._state === L.Marker.MovingMarker.destroyedState;
}

destroy: function() {
    if (this.isDestroyed()) {
        return;
    }
    this._state = L.Marker.MovingMarker.destroyedState;
    this._removeMarker();
},

onEnd: function(map) {
    L.Marker.prototype.onEnd.call(this, map);

    if (this.options.remove && (this.isDestroyed())) {
        this.destroy();
        return;
    }
    this._removeMarker();
},

 _removeMarker: function() {
    this._state = L.Marker.MovingMarker.destroyedState;
    if (this._animRequested) {
        L.Util.cancelAnimFrame(this._animId);
        this._animRequested = false;
    }
    this.L.Marker.MovingMarker.clearLayers(this, map);
    this.L.Marker.MovingMarker = null;
},

Unfortunately, for some reason the method clearLayers isn't doing its job here.

I've a working example here

jw0903 commented 5 years ago

@pierrebonbon did you fix your problem? I have a problem about remove the movingMarker?

ghost commented 5 years ago

Hi @lin559 unfortunately no, I didn't advance on this issue any further I'm afraid. It works pretty well with only one marker but didn't manage to make it work with an array.

Gerrist commented 2 years ago

Any updates on this issue?