I'm trying to get this function on my custom Leaflet map https://rodadagua.netlify.app/
I've found some very close examples and questions on here but being a complete novice unfortunately I have no idea what I´m doing.
This is essentially the same question https://stackoverflow.com/questions/17997247/change-and-change-back-leaflet-icon-on-click but with such basic knowledge I can't work it out. Also I found this question https://stackoverflow.com/questions/61890461/leaflet-map-on-click-changing-color-of-all-markers and example https://jsfiddle.net/vz7jmnqs/2/ is essentially what I'm after but again I can't work out how to include it in my code and make it work.
I want to change marker icon when clicked (so that it shows a different coloured version) and then return back to original state (original icon/colour) when click elsewhere, anywhere, eg. close the associated popup or click on the map or click on another marker.
The closet I've got to is I can change the icon on a click, but I want to change it back.
var defaultIcon = L.icon({
iconUrl: 'scripts/leaflet/images/marker-icon.png',
shadowUrl: 'scripts/leaflet/images/marker-shadow.png',
});
var brownIcon = L.icon({
iconUrl: 'scripts/leaflet/images/marker-icon-brown.png',
shadowUrl: 'scripts/leaflet/images/marker-shadow.png',
});
var testmarker = L.marker([0.0, 0.0], {icon: defaultIcon}).addTo(map);
testmarker.on('click', function(e) {
testmarker.setIcon(brownIcon);
});
I'm using the basic Leaflet script to define the map etc. then all of my markers and popups are set up like this example:
var marker_Waldemar = L.marker([15.0, -40.0]).addTo(map);
marker_Waldemar.bindPopup('<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>', {
maxWidth : 520,
maxHeight : 450,
});
I would like to know exactly what code I need to put in and where.