Utilizing ajax to constantly update the markers' location every 10 seconds using PruneCluster.
(function update() {
$.ajax({
url: 'https://wanderdrone.appspot.com/',
dataType: 'json',
success: function (data) {
var leafletView = new PruneClusterForLeaflet();
leafletView.BuildLeafletClusterIcon = function (cluster) {
var e = new L.Icon.MarkerCluster();
e.stats = cluster.stats;
e.population = cluster.population;
cluster.ENABLE_MARKERS_LIST = true;
return e;
};
var markers = [];
var myServerData = data;
console.log(data);
map.setView(new L.LatLng((myServerData.geometry.coordinates[1]), (myServerData.geometry.coordinates[0])), 12);
var marker = new PruneCluster.Marker((myServerData.geometry.coordinates[1]), (myServerData.geometry.coordinates[0]));
markers.push(marker);
leafletView.RegisterMarker(marker);
leafletView.ProcessView();
map.addLayer(leafletView);
}
})
.then(function () {
setTimeout(update, 10000);
});
})();
Although I successfully update the locations of new markers, they keep overlaying the previous ones without replacing them. The new markers and clusters simply accumulate on top of the old ones. How can I remove the previous markers' layer before adding the new one?