Откройте всплывающие окна всех маркеров, которые видны на карте с масштабом> 10 - PullRequest
0 голосов
/ 18 апреля 2019

Мне нужно открыть все маркеры, которые видны на карте в 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)

1 Ответ

0 голосов
/ 18 апреля 2019

Вы должны добавить свои маркеры в массив до / после добавления их на карту, чтобы получить к ним легкий доступ.

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();
}
...