Sono abbastanza nuovo alla libreria Leaflet e JavaScript in generale, e sono bloccato cercando di capire come mostrare/nascondere un opuscolo Etichetta basata sul livello di zoom (e i marcatori sono in un 'cluster' strato).Come faccio a mostrare un'etichetta oltre un certo livello di zoom in Leaflet?
I marcatori sono tutti caricati tramite callback AJAX e quindi associo il popup e l'etichetta utilizzando lo onEachFeature
, quindi aggiungo il livello dei marcatori geoJson alla mappa.
Mi piacerebbe mostrare le etichette solo quando ingrandito ad un certo livello, ma non ho avuto fortuna. Ho anche provato ad aggiungere il leaflet.zoomcss.js
ma suppongo che non lo sto usando correttamente.
Esempio
var officesLayerGroup = L.markerClusterGroup();
var currentMakers;
function DiaplyLocalOffices(jqOffices) {
currentMakers = new L.geoJson(jqOffices, {
style: function (feature) {
var c = feature.properties.markercolor;
if (feature.properties.OfficeID == 0) {
c = 'yellow';
}
return { color: c };
},
pointToLayer: function (feature, latlng) {
return new L.CircleMarker(latlng, { radius: 7, fillOpacity: 0.5 });
},
onEachFeature: bindOfficePopup
});
officesLayerGroup.addLayer(currentMakers);
map.addLayer(officesLayerGroup);
}
function bindOfficePopup(feature, layer) {
// This function adds the popup based on the information in the 'layer' or marker
// Keep track of the layer(marker)
feature.layer = layer;
var props = feature.properties;
if (props) {
var desc = '<span id="feature-popup">';
//.. a bunch of other html added here!
var warn = props.Warning ? props.Warning : null;
if (warn !== null) {
desc += '<font size="4" color="red"><strong><em>' + warn + '</em></strong></font></br>';
}
desc += '</span>';
layer.bindPopup(desc);
layer.bindLabel('Hi Label!', { noHide: true, className: 'my-css-styled-labels'});
}
}
Ho anche provato ad aggiungere in questo modo, ma che non ha funzionato neanche:
layer.on({
zoomend: showLabel(e)
});
e quindi una funzione sveltina:
function showLabel(e) {
z = map.getZoom();
if (z > 6) {
layer.bindLabel("HIYA", { noHide: true, className: 'my-css-styled-labels' });
}
}
Ma senza fortuna, anche quando aggiungi la libreria e gli stili CSS per leaflet.zoomcss.js
Ci scusiamo per il lungo, ma qualsiasi aiuto sarebbe davvero apprezzato!
Non so quale sia il valore aggiunto al mio post sopra, tranne che il tuo funziona solo con uno strato geoJson specifico, mentre il mio funziona su mappe con qualsiasi tipo e numero di livelli. –