Skip to main content
correcting code, was missing an underscore
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

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, txt_Waldemar, 520, 450);
marker_Waldemar.addTo(map);

edit: corrected missing underscore on txtWaldemar > txt_Waldemar

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, txt_Waldemar, 520, 450);
marker_Waldemar.addTo(map);

edit: corrected missing underscore on txtWaldemar > txt_Waldemar

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, txt_Waldemar, 520, 450);
marker_Waldemar.addTo(map);

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, txtWaldemartxt_Waldemar, 520, 450);
marker_Waldemar.addTo(map);

edit: corrected missing underscore on txtWaldemar > txt_Waldemar

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);

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, txt_Waldemar, 520, 450);
marker_Waldemar.addTo(map);

edit: corrected missing underscore on txtWaldemar > txt_Waldemar

added 59 characters in body
Source Link
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 (without popup options, these could be additional function parameters):

var marker_Waldemartxt_Waldemar = defineMarker([15.0, -40.0], defaultIcon, brownIcon, '<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);

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) {
  var markerIcon = icon;
  var marker = L.marker(coords, {
    icon: icon
  }).bindPopup(popupTxt);
  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 (without popup options, these could be additional function parameters):

var marker_Waldemar = defineMarker([15.0, -40.0], defaultIcon, brownIcon, '<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>');
marker_Waldemar.addTo(map);

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);
deleted 40 characters in body
Source Link
TomazicM
  • 27k
  • 23
  • 31
  • 42
Loading
deleted 112 characters in body
Source Link
TomazicM
  • 27k
  • 23
  • 31
  • 42
Loading
Source Link
TomazicM
  • 27k
  • 23
  • 31
  • 42
Loading