1

I'm fairly new to leaflet and web mapping in general. I was curious if there is a way to set labels to only appear at a certain range of extents. The point data layer I have clusters too much preventing any legible labels. It also makes the map look really cluttered.

I have been using the leaflet.label plugin but will use others if needed.

Using Leaflet 0.7

grahamfw
  • 65
  • 1
  • 10
  • 1
    Yes, listen for map 'zoomend' and then assess zoom level with map.getZoom and then add your labels. See how to do that in a similar question here, http://gis.stackexchange.com/questions/41928/adding-removing-leaflet-geojson-layers – timlohnes Aug 26 '15 at 16:36

1 Answers1

1

I was able to set the extents for labels not through plugins, but through a tweaked version of the suggestion made by Tim. Here is my code:

    var visible;

    map.on('zoomend', function (e) {
        if (map.getZoom() > 13) {
            if (!visible) {
                yourlayer.eachLayer(function (layer) {
                    layer.showLabel();
                });
                visible = true;
            }
        } else {
            if (visible) {
                yourlayer.eachLayer(function (layer) {
                    layer.hideLabel();
                });
                visible = false;
            }
        }
    });
grahamfw
  • 65
  • 1
  • 10