Search code examples
javascriptangularleafletangular-leaflet-directiveleaflet.markercluster

Update Leaflet Marker


it's about leaflet marker on angular 5 , the marker value is dynamic and I need to draw just the last result. How can I do it please?

var markers;
markers = new L.LayerGroup().addTo(myfrugalmap);
let timer = TimerObservable.create(0, 10000);
this.subscription = timer.subscribe(t => { this.MyService.Localize().subscribe( result => {
this.positions = result;
let xpo = this.positions.x;
let ypo = this.positions.y;
let mar=L.marker([xpo,ypo], {icon: greenIcon}) mar.addTo(markers) });

//markers.clearLayers();
} )

in result (without (markers.clearLayers)) I have evry 10 second the new position on the map and I need to drop the old positions and the see just the last


Solution

  • Did you try clearing before you added the newly created marker to the layer? Your last few lines would look something like this.

    let mar=L.marker([xpo,ypo], {icon: greenIcon});
    markers.clearLayers();
    mar.addTo(markers);