I need to open all the markers that are visible on the map at Zoom > 10. I am using leaflet.markercluster plugin.
Init map:
initMap() {
this.map = L.map('map', {
center: [55.55, 37.61],
zoom: 9,
layers: this.layer
})
this.tileLayer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution:
'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, ©'
})
this.tileLayer.addTo(this.map)
this.map.on('zoom', function(ev) {
???
})
Add marker layer:
this.markerLayer = new L.LayerGroup() // layer contain searched elements
// this.map.addLayer(this.markerLayer)
for (const i in data) {
...
const marker = new L.Marker(new L.latLng(loc), { title: title, icon: icon })// se property searched
marker.bindPopup(title)
this.markerLayer.addLayer(marker)
}
Use leaflet marker cluster:
this.markersLayer = L.markerClusterGroup({
iconCreateFunction: function(cluster) { ... },
singleMarkerMode: false
})
this.markersLayer.addLayer(this.markerLayer)
this.map.addLayer(this.markersLayer)
Best Answer
I couldn't find any other method than to loop through all the markers at each view change (zoom, move) and show/hide popup for visible markers depending on zoom level.
Base of the code is taken from Leaflet.markercluster plugin example. Zoom level > 16 is set as trigger for popup display.
Important options:
closePopupOnClick: false
to prevent automativ popup close on map clickautoClose: false
to prevent close of popup when another popup is opened,autoPan: false
to prevent panning of map when popup is not entierly inside mapProbably not everything was taken care of here, just the basic concept.