簡體   English   中英

具有Markercluster的Mapbox.js GEOJson

[英]Mapbox.js GEOJson with Markercluster

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

沒有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與GEOJson相結合? 提前致謝

//編輯:找到一個不同的示例,該示例已經可以使用:

      var markers = L.markerClusterGroup();

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

          }
       });
       markers.addLayer(geoJsonLayer);

       map.addLayer(markers);    

       map.fitBounds(markers.getBounds());

但是現在,它使用諸如“ marker-color”-“ marker-size”之類的屬性來放松我的“默認”樣式。

我現在該如何為標記設置樣式?

Mapbox有點奇怪,看起來總是有至少10種方法可以解決此類問題;)

如果您的markers變量是一個數組(在其上使用.push() ),則不能使用clusterGroup.addLayer(markers) 您可能已經使用過.addLayers(markers) (在addLayers上帶有“ s”)。

我想您無論如何都不想在每個"layeradd"事件上添加整個數組,所以也許您的意思是clusterGroup.addLayer(marker) (在marker上沒有“ s”,即您的單獨標記/功能)。

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

最后, 也不要將要素組也添加到地圖中。 那是當前添加您唯一標記的標記。 您想讓您的集群組自動處理從地圖添加/刪除標記。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM