I'm retrieving data json api content on coordinates GPS then using into leaflet map markers . I'm having trouble to clearing the markers when i have new updates from data json api ! , all markers are duplicate when i have new update .
Code
datas(){
interval(3000).subscribe(()=>{
this.http.get("xxxxxxxxxxxxx",{},{}).then((data) => {
this.Data = JSON.parse(data.data);
this.points = Object.keys(this.Data)
.map(key => this.Data[key])
.map((position) => ({
lat: position[1],
lng: position[2],
})).filter(position => position.lat && position.lng ).forEach(i=>{
console.log(i.lat,i.lng)
new L.Marker([i.lat,i.lng], {
icon: new L.DivIcon({
html: `<div style="width: 65px;";>
<img src="assets/aeroplane.png"/style="width: 20px;height: 25px;">
</div>`
})
}).addTo(this.map)
})
this.map.removeLayer()
});
})
}
How i can remove the markers then add new markers on map after updating data and avoid duplicate markers