Solution below is based on assumption that each marker will have a popup that opens upon marker click.
Logic goes as follows:
- when marker is clicked, popup opens and marker icon is changed;
- when marker popup is closed (it can be by the same marker click, popup close button click, another marker click, anywhere on map click), marker icon is set back to original icon.
Most economic way to do this is to define marker creation function, where all necessary things are taken care of:
- marker created, on desired coordinates and with desired icon and popup;
- processing function are defined for
popupupen
andpopupclose
marker events, where change of marker icon is taken care of.
Code for such a marker creating function could then look something like this:
function defineMarker(coords, icon, selectedIcon, popupTxt, popupWidth, popupHeight) {
var markerIcon = icon;
var marker = L.marker(coords, {
icon: icon
}).bindPopup(popupTxt, {
maxWidth: popupWidth,
maxHeight: popupHeight
});
marker.on('popupopen', function(evt) {
marker.setIcon(selectedIcon);
});
marker.on('popupclose', function(evt) {
var marker = evt.target;
marker.setIcon(markerIcon);
});
return(marker);
}
Creation of your marker_Waldemar
marker could then look like this:
var txt_Waldemar = '<h3>EMEF Prof. Waldemar Rodrigues</h3><p><iframe src="https://galeriafeira.netlify.app/" width="500" height="354" style="border:none;"></iframe></p><p><b>Feira de Ciências</b></p>'
var marker_Waldemar = defineMarker([15.0, -40.0], defaultIcon, brownIcon, txtWaldemar, 520, 450);
marker_Waldemar.addTo(map);