使用缩放> 10打开地图上可见的所有标记的弹出窗口

问题描述 投票:0回答:1

我需要打开Zoom,10上地图上可见的所有标记。我还使用leaflet.markercluster。

初始地图:

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:
      '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, &copy;'
  })
  this.tileLayer.addTo(this.map)

this.map.on('zoom', function(ev) {
    ???
  })

添加标记图层:

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)
  }

使用传单标记群集:

this.markersLayer = L.markerClusterGroup({
    iconCreateFunction: function(cluster) { ... },
    singleMarkerMode: false
  })
  this.markersLayer.addLayer(this.markerLayer)
  this.map.addLayer(this.markersLayer)
popup leaflet zoom marker leaflet.markercluster
1个回答
0
投票

您应该在将数据添加到地图之前/之后将标记添加到数组中以便轻松访问它们。

var markers = [];

for (const i in data) {
    const marker = new L.Marker(new L.latLng(loc), { title: title, icon: icon })
    marker.bindPopup(title)
    this.markerLayer.addLayer(marker)
    markers.push(marker);
}

之后,您可以循环遍历标记数组并使用marker的openPopUp函数以编程方式打开标记的弹出窗口。

for(i = 0; i< markers.length;i++){
    markers[i].openPopup();
}
© www.soinside.com 2019 - 2024. All rights reserved.