2017-10-06 84 views
1

我的應用程序中有以下代碼,所有標記都添加到一個組中。我希望在加載地圖時,羣集中未摺疊的標記彈出窗口顯示它們的彈出窗口。 (標記,其是羣集的一部分,但不折疊成集羣上的特定縮放級別,當映射在該縮放等級加載)傳單markercluster加載時打開彈出框

var markers = [{ 
     "latLong": [57.67, -3.89] 
     }, 
    { 
     "latLong": [-4.4, -58.34] 
     }, 
    { 
     "latLong": [35.79, 139.48] 
     }], 
markerGroup = L.markerClusterGroup(), 
marker; 

markers.forEach(function (markerConfig, index) { 

marker = new L.Marker(new L.LatLng(markerConfig.latLong[0], markerConfig.latLong[1])); 
marker.bindPopup(index, { 
    "autoClose": false, 
    "closeOnClick": false 
}).openPopup(); 
markerGroup.addLayer(marker); 
}); 

clusterMap.addLayer(markerGroup); 

我想上標記的openPopup()方法,以打開彈出當加載地圖時,它會在點擊標記時打開。請幫忙。

回答

0

我找到了一個解決方案,訣竅是首先將羣集添加到地圖,然後將標記添加到該羣組,然後在標記上調用openPopup。所以這只是一個調用函數的順序問題。

var markers = [{ 
      "latLong": [57.67, -3.89] 
      }, 
     { 
      "latLong": [-4.4, -58.34] 
      }, 
     { 
      "latLong": [35.79, 139.48] 
     }], 
    markerGroup = L.markerClusterGroup(), 
    marker; 
clusterMap.addLayer(markerGroup); 

markers.forEach(function (markerConfig, index) { 
    marker = new L.Marker(new L.LatLng(markerConfig.latLong[0], markerConfig.latLong[1])); 
    markerGroup.addLayer(marker); 
    marker.bindPopup(index, { 
     "autoClose": false, 
     "closeOnClick": flase 
    }).openPopup(); 
});