Skip to main content
edited body
Source Link
TomazicM
  • 27k
  • 23
  • 31
  • 42

To avoid dealing with each marker separately, you can put those labels that you want to show only at certain zoom level, not directly on map but in a separate group layer. You can then add/remove this group layer to the map depending on zoom.

Code could then look something like this:

var markerGroup = L.layerGroup();

function createLabel(layer, text, count){
  .
  .
  .
  var label = L.marker(layer.getLatLng(),{icon: icon});
  layer.appendedLabel = label;
  if (count == 1)
    label.addTo(mymap);
  else {
    label.addTo(markerGroup);
  }
  .
  .
  .
}

var show_label_zoom = 20;      
var currZoom = mymap.getZoom();

function show_hide_labels() {
  var zoom = mymap.getZoom();
  if (zoom > currZoom) {
    if (zoom == show_label_zoom) markerGroup.addTo(mymap);
    }
  else {
    if (currZoom == show_label_zoom) markerGroup.removeFrom(mymap);
  }
  currZoom = mymap.getZoom();
}

var show_label_zoom = 20;      
var currZoom = mymap.getZoom();

mymap.on('zoomend', show_hide_labels);
if (currZoom >= show_label_zoom) markerGroup.addTo(mymap);

To avoid dealing with each marker separately, you can put those labels that you want to show only at certain zoom level, not directly on map but in a separate group layer. You can then add/remove this group layer to the map depending on zoom.

Code could then look something like this:

var markerGroup = L.layerGroup();

function createLabel(layer, text, count){
  .
  .
  .
  var label = L.marker(layer.getLatLng(),{icon: icon});
  layer.appendedLabel = label;
  if (count == 1)
    label.addTo(mymap);
  else {
    label.addTo(markerGroup);
  }
  .
  .
  .
}

function show_hide_labels() {
  var zoom = mymap.getZoom();
  if (zoom > currZoom) {
    if (zoom == show_label_zoom) markerGroup.addTo(mymap);
    }
  else {
    if (currZoom == show_label_zoom) markerGroup.removeFrom(mymap);
  }
  currZoom = mymap.getZoom();
}

var show_label_zoom = 20;      
var currZoom = mymap.getZoom();

mymap.on('zoomend', show_hide_labels);
if (currZoom >= show_label_zoom) markerGroup.addTo(mymap);

To avoid dealing with each marker separately, you can put those labels that you want to show only at certain zoom level, not directly on map but in a separate group layer. You can then add/remove this group layer to the map depending on zoom.

Code could then look something like this:

var markerGroup = L.layerGroup();

function createLabel(layer, text, count){
  .
  .
  .
  var label = L.marker(layer.getLatLng(),{icon: icon});
  layer.appendedLabel = label;
  if (count == 1)
    label.addTo(mymap);
  else {
    label.addTo(markerGroup);
  }
  .
  .
  .
}

var show_label_zoom = 20;      
var currZoom = mymap.getZoom();

function show_hide_labels() {
  var zoom = mymap.getZoom();
  if (zoom > currZoom) {
    if (zoom == show_label_zoom) markerGroup.addTo(mymap);
    }
  else {
    if (currZoom == show_label_zoom) markerGroup.removeFrom(mymap);
  }
  currZoom = mymap.getZoom();
}

mymap.on('zoomend', show_hide_labels);
if (currZoom >= show_label_zoom) markerGroup.addTo(mymap);
Source Link
TomazicM
  • 27k
  • 23
  • 31
  • 42

To avoid dealing with each marker separately, you can put those labels that you want to show only at certain zoom level, not directly on map but in a separate group layer. You can then add/remove this group layer to the map depending on zoom.

Code could then look something like this:

var markerGroup = L.layerGroup();

function createLabel(layer, text, count){
  .
  .
  .
  var label = L.marker(layer.getLatLng(),{icon: icon});
  layer.appendedLabel = label;
  if (count == 1)
    label.addTo(mymap);
  else {
    label.addTo(markerGroup);
  }
  .
  .
  .
}

function show_hide_labels() {
  var zoom = mymap.getZoom();
  if (zoom > currZoom) {
    if (zoom == show_label_zoom) markerGroup.addTo(mymap);
    }
  else {
    if (currZoom == show_label_zoom) markerGroup.removeFrom(mymap);
  }
  currZoom = mymap.getZoom();
}

var show_label_zoom = 20;      
var currZoom = mymap.getZoom();

mymap.on('zoomend', show_hide_labels);
if (currZoom >= show_label_zoom) markerGroup.addTo(mymap);