2013-08-05 70 views
2

使用George MacKerron的偉大蜘蛛https://github.com/jawj/OverlappingMarkerSpiderfier-Leaflet 存在問題。Leaflet標記單擊回撥在spiderfy上

當點擊一個別針時,它的彈出窗口顯示出來。這很酷,只要我沒有重疊的引腳,我就是蜘蛛俠。 spiderfied pin的問題是彈出窗口,它也會在第一次點擊時打開,而它與spiderfied集合的某些其他針腳重疊。

因此,我需要一個對spiderfy-listener的點擊回調,它允許我在spiderfying之後立即關閉彈出窗口。或者甚至更好,直接在Spiderfying集之前。

現在的問題是:我如何實現對spiderfy-listener的回調?那麼,也許它是愚蠢的做那件事,那麼請告訴我該怎麼做。謝謝:)

我的代碼,它使用一個怪誕的20ms的劈在底部,我不希望保留:

// Kartendarstellung mit Spiderfier 
    var map = L.map('basicMap').setView(new L.LatLng(position[0][0], position[0][1]), 13); 
    map.doubleClickZoom.disable(); 
    var oms = new OverlappingMarkerSpiderfier(map, { 
     keepSpiderfied: true, 
     nearbyDistance: 25, 
     legWeight: 2 
    }); 

    L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', { 
     maxZoom: 18, 
     attribution: 'Kartendaten &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> und Mitwirkende, Lizenz: <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Bilddaten © <a href="http://cloudmade.com">CloudMade</a>' 
    }).addTo(map);  

    for (var i = 1; i < <?php echo count($pos);?>; i++){ 
     switch(position[i][3]){ 
      case "B": 
       marker = L.marker([position[i][0], position[i][1]],{icon: BIcon}).addTo(map); 
       break; 
      case "S": 
       marker = L.marker([position[i][0], position[i][1]],{icon: SIcon}).addTo(map); 
       break;  
     } 
     // Marker ungeöffnet auf Karte setzen 
     var popup = new L.popup(); 
     var content = position[i][2]; 
     // Marker-Inhalt zuweisen 
     marker.bindPopup(content); 
     // Spiderfier Marker setzen 
     oms.addMarker(marker); 
    } 

    oms.addListener('spiderfy', function() { 
     // Hack als Ersatz zu fehlendem Spiderfy-Marker-Click-Callback 
     setTimeout(function() { 
      map.closePopup(); 
     }, 20); 
    }); 

回答

0

我相信問題是,你是直接綁定彈出窗口標記時,你創建它,而不是在點擊oms圖層時動態綁定它。這是OMS文檔https://github.com/jawj/OverlappingMarkerSpiderfier-Leaflet

我沒有測試過這在推薦的方法,但你的代碼應該是這個樣子:

for (var i = 1; i < <?php echo count($pos);?>; i++){ 
    switch(position[i][3]){ 
     case "B": 
      marker = L.marker([position[i][0], position[i][1]],{icon: BIcon}).addTo(map); 
      break; 
     case "S": 
      marker = L.marker([position[i][0], position[i][1]],{icon: SIcon}).addTo(map); 
      break;  
    } 

    // In your loop, store the popup content as a property of the marker 
    marker.desc = position[i][2]; 

    // Spiderfier Marker setzen 
    oms.addMarker(marker); 
} 

// Delegate the click to the oms layer and dynamically create the popup based on the 
var popup = new L.Popup(); 
oms.addListener('click', function(marker) { 
    // Set the popup content using the marker property set in the loop 
    popup.setContent(marker.desc); 
    popup.setLatLng(marker.getLatLng()); 
    map.openPopup(popup); 
}); 
相關問題