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

时间:2019-04-18 11:47:06

标签: popup leaflet zoom marker leaflet.markercluster

  

我需要打开“缩放”在地图上可见的所有标记,   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)

1 个答案:

答案 0 :(得分: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);
}

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

for(i = 0; i< markers.length;i++){
    markers[i].openPopup();
}