Here are what I think are the pertinent bits of the code...
//creates the map
//calls setupLabelControl to create a div containing the tickbox that toggles popup visibility
function setupMap() {
map = L.map('mapid', {fullscreenControl: true}).setView([latitude, longitude], 7);
mapLayer = L.tileLayer('http://{s}.
basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
{attribution: '© OpenStreetMap contributors, © CartoDB'});
setupLabelControl();
}
//adds a checkbox control which allows popup visibility to be toggled
function setupLabelControl() {
var command = L.control({position: 'bottomleft'});
command.onAdd = function (map) {
var div = L.DomUtil.create('div', 'toggleLabelDiv');
div.innerHTML = '<form><input id="toggleLabel" type="checkbox"/><label for="chkcmd">Enable Labels</label></form>';
div.style.backgroundColor = "white";
div.style.padding = "6px 6px 6px 6px";
return div;
};
command.addTo(map);
//adds an event listener which calls toggleTrackLabels when the checkbox is clicked
document.getElementById("toggleLabel").addEventListener("click", toggleTrackLabels, false);
}
//iterates over an array of all markers and opens or closes the associated popup
function toggleTrackLabels() {
if (document.getElementById("toggleLabel").checked == true) {
markers.forEach((value, key) => {
var marker = markers.get(key);
marker.openPopup();
})
}
else {
markers.forEach((value, key) => {
var marker = markers.get(key);
marker.closePopup();
})
}
}
//following code is used to actually create the marker
//popups get displayed if the mouse is hovered over the marker
//and closed when the mouse is moved off the marker
//finally each marker is added to an array for use by the functions above
var marker = L.marker([aircraft.lat, aircraft.long], {icon:aircraft.trackIcon, autoPan: false})
.addTo(map).bindPopup(aircraft.displayData,
{closeButton:false, closeOnClick:false, autoClose: false, maxWidth: 60, className:'stylePopup'});
marker.on('mouseover', function(e){
this.openPopup();
});
marker.on('mouseout', function(e){
this.closePopup();
});
markers.set(
aircraft.id, marker);
The issue I am seeing occurs when the user clicks the checkbox which was created in setupLabelControl() and thus the event listener invokes toggleTrackLabels() which iterates over all markers and calls marker.openPopup()