12

I would like to show icons if the user zooms to level 7 or more and hide the markers when zoom level is 6 or lower. I've tried using the 'zoomend' property in my function like so:

var shelter1 = L.marker([55.1, 11.6], {icon: shelterIcon}).addTo(map);

    map.on('zoomend', function() {
        var currentZoom = map.getZoom();
        if(currentZoom >= 7) {
          shelter1.bindPopup("Shelter");
        }
        else {
          shelter1.hide();
        }
    });

I know that hide() is not valid. But I need something similar. Couldn't find anything in the Leaflet documentation.

Vince
  • 20,017
  • 15
  • 45
  • 64
Saud
  • 711
  • 4
  • 10
  • 22

1 Answers1

22

All right. I came up with a solution using a layer:

var shelter1 = L.marker([55.08, 11.62], {icon: shelterIcon});

var shelterMarkers = new L.FeatureGroup();

shelterMarkers.addLayer(shelter1);

map.on('zoomend', function() {
    if (map.getZoom() <7){
            map.removeLayer(shelterMarkers);
    }
    else {
            map.addLayer(shelterMarkers);
        }
});
Roberto
  • 103
  • 3
Saud
  • 711
  • 4
  • 10
  • 22