Skip to main content
4 of 6
added 59 characters in body
TomazicM
  • 27k
  • 23
  • 31
  • 42

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 and popupclose 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&ecirc;ncias</b></p>'
var marker_Waldemar = defineMarker([15.0, -40.0], defaultIcon, brownIcon, txtWaldemar, 520, 450);
marker_Waldemar.addTo(map);
TomazicM
  • 27k
  • 23
  • 31
  • 42