I am creating around 300 markers on Leaflet map and every 5 seconds I am getting new data from the server and I'm removing markers from the map and adding markers with new data. But the problem is that browser freezes after several minutes (around 20 or 30 minutes).
Here is my code for adding markers on the map
var map = L.map('map').setView([48.48106, -123.3921], 13);
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution:
'© <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
var mapHelper = new OpenStreetMapsHelper(map);
function showMarkers() {
mapHelper.removeEntities('Vehicles:0', true);
for (let i = 0; i < vehicleData.length; i++) {
let bearing = Math.floor(Math.random() * 360 + 1);
let dataItem = vehicleData[i];
let vehicleBubble = mapHelper.createPushpin(
[dataItem.lat, dataItem.lng],
mapHelper.vehiclePushpinOptions(bearing, 1),
{
mouseover: function (e) {
console.log('mouse over');
},
mouseout: function (e) {
console.log('mouse out');
},
}
);
mapHelper.addEntity(vehicleBubble, 'Vehicles:0', 9999);
}
setTimeout(showMarkers, 5000);
}
showMarkers();
The whole code is bit longer so I am adding all my code here as I have to keep code minimal. Please see working example here https://embed.plnkr.co/plunk/tZOC6bTmUfLvvNJi
I took little help from @TomazicM to generate marker positions.
I have to change marker position as well as marker icon because marker icon changes depending on vehicle bearing.