2016-08-23 51 views
0

我想用mapbox.js實現帶有MarkerCluster的GEOJson。Mapbox.js GEOJson Markercluster

沒有Markercluster,一切工作正常:

$.ajax({ 
     url: 'dashboard/geoJSON', 
     dataType: 'json', 
     type: 'POST', 
     success: function(geojson) { 

      locations = L.mapbox.featureLayer().addTo(map) 
      locations.on('layeradd', function (e) { 
       var marker = e.layer; 
       markers.push(marker); 
      }); 

      locations.setGeoJSON(geojson); 

      ... 

但當生病嘗試實行MarkerCluster,那隻能說明1標記

 ... 
     success: function(geojson) { 

      locations = L.mapbox.featureLayer().addTo(map) 
      var clusterGroup = new L.MarkerClusterGroup(); 
      locations.on('layeradd', function (e) { 
       var marker = e.layer; 
       markers.push(marker); 
       clusterGroup.addLayer(markers); 
      }); 
      locations.setGeoJSON(geojson); 
      map.addLayer(clusterGroup); 

,並顯示我下面的錯誤:

TypeError: t.onAdd is not a function 

任何人都可以幫助我,我怎樣才能成功地將markercluster與GEOJs結合從遠程服務器上打開? 在此先感謝

//編輯:找到一個不同的例子,它已經使用的作品:

 var markers = L.markerClusterGroup(); 

     var geoJsonLayer = L.geoJson(geojson, { 
      onEachFeature: function (feature, layer) { 

      } 
     }); 
     markers.addLayer(geoJsonLayer); 

     map.addLayer(markers);  

     map.fitBounds(markers.getBounds()); 

但現在失去我的「默認」的造型中包含「標誌色」屬性 - 「標識尺寸」。

我該如何設計我的標記?

Mapbox有點怪異,看起來總有至少10種方法來解決這樣的事情;)

回答

1

如果您markers變量是一個數組(如你使用.push()就可以了),你不能使用clusterGroup.addLayer(markers)。您可以使用.addLayers(markers)(在addLayers上使用「s」)。

我猜你不希望整個陣列在每個"layeradd"事件仍要添加,所以也許你(上,即你的個人標記/功能不帶「s」)的意思clusterGroup.addLayer(marker)

順便說一句,您可以使用L.mapbox.featureLayer()onEachFeature選項,而不是使用事件偵聽器。

最後,不要添加您的功能組以及地圖。那是目前添加唯一標記的那個。您希望讓羣集組自動處理從地圖添加/刪除標記。

+0

感謝您的回答,我修改了一下我的問題。 – derdida

+0

你知道嗎我可以重置一個markercluster嗎?例如:我在用戶更改過濾器時再次加載標記,然後我想重新初始化羣集組。正常使用情況:markers.forEach(函數(entry){map.removeLayer(entry); }); ,但這不再有效。有任何想法嗎? – derdida

+0

我不知道我明白你的意思......你可以試試'clusterGroup.clearLayers()'。如果這不是你正在尋找的東西,也許你應該打開一個有關更多細節的專門問題。 – ghybs