2016-11-19 1 views
0

현재 하위 그룹이 제대로 작동하는 데 문제가 있습니다. 아래 코드를 사용하면 클러스터를 표시 할 수 있지만 클러스터를 해제하면 그에 따라 업데이트되지 않습니다. 또한 레이어를 켜면 기본 포인트가 모두 켜집니다. 하위 그룹 레이어를 제대로 작동 시키려면 어떻게해야합니까?전단/CartoDB L.featureGroup.subGroup()

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Map</title> 

    <meta charset="UTF-8"> 

    <!-- If IE use the latest rendering engine --> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <!-- Set the page to the width of the device and set the zoon level --> 
    <meta name="viewport" content="width = device-width, initial-scale = 1"> 

    <!-- jquery --> 
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> 

    <!-- Leaflet --> 
    <link rel="stylesheet" href="http://unpkg.com/[email protected]/dist/leaflet.css" /> 
    <script src="http://unpkg.com/[email protected]/dist/leaflet.js"></script> 
    <script src="leaflet.featuregroup.subgroup.js"></script> 
    <script src="https://unpkg.com/[email protected]/dist/leaflet.featuregroup.subgroup.js"></script> 


</head> 
<body> 

    <link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/Leaflet/Leaflet.markercluster/v1.0.0-beta.2.0/dist/MarkerCluster.Default.css"> 
    <link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/Leaflet/Leaflet.markercluster/v1.0.0-beta.2.0/dist/MarkerCluster.css"> 
    <script src="https://cdn.rawgit.com/Leaflet/Leaflet.markercluster/v1.0.0-beta.2.0/dist/leaflet.markercluster.js"></script> 

    <div id="map" style="height: 800px"></div> 

    <script> 
     var map = L.map('map').setView([38.607, -97.277], 5); 

     var OpenMapSurfer_Grayscale = L.tileLayer('http://korona.geog.uni-heidelberg.de/tiles/roadsg/x={x}&y={y}&z={z}', 
      { 
      maxZoom: 19, 
      attribution: 'Imagery from <a href="http://giscience.uni-hd.de/">' + 
      'GIScience Research Group @ University of Heidelberg</a> &mdash; Map data &copy; ' + 
      '<a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>' 
     }).addTo(map); 

     //attributes for basemap credit (lower right hand corner annotation) 
     var streetsAttr = 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'; 
     var aerialAttr = 'Tiles &copy; Esri &mdash; Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'; 
     var artsyfartsyAttr = 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'; 

     //crete variables for the base map layer switcher 
     var streets = L.tileLayer('http://stamen-tiles-{s}.a.ssl.fastly.net/terrain/{z}/{x}/{y}.png', { 
      id: 'MapID', 
      attribution: streetsAttr 
      }), 
      aerial = L.tileLayer('http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', { 
      id: 'MapID', 
      attribution: aerialAttr 
      }), 
      artsyfartsy = L.tileLayer('http://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}.png', { 
      id: 'MapID', 
      attribution: artsyfartsyAttr 
      }); 

     //create baseMaps variable to store basemap layer switcher 
     var baseMaps = { 
      "Streets": streets, 
      "Aerial": aerial, 
      "ArtsyFartsy": artsyfartsy 
     }; 
     var masClusGroup = new L.markerClusterGroup().addTo(map); 

     var efuSub = L.featureGroup.subGroup(masClusGroup).addTo(map); 
     var f1Sub = L.featureGroup.subGroup(masClusGroup).addTo(map); 
     var f2Sub = L.featureGroup.subGroup(masClusGroup).addTo(map); 

     var sqlEFU = "https://kmitch24.carto.com/api/v2/sql?format=GeoJSON&q=SELECT * FROM tornadodata_beginningpoint where tor_f_scal = 'EFU'"; 
     var sqlF1 = "https://kmitch24.carto.com/api/v2/sql?format=GeoJSON&q=SELECT * FROM tornadodata_beginningpoint where tor_f_scal = 'F1'"; 
     var sqlF2 = "https://kmitch24.carto.com/api/v2/sql?format=GeoJSON&q=SELECT * FROM tornadodata_beginningpoint where tor_f_scal = 'F2'"; 

     $.getJSON(sqlEFU, function(cartodbdata) { 
      geojsonlayer= L.geoJson(cartodbdata, { 
       onEachFeature: function (feature, layer) { 
        layer.bindPopup("F-Scale: " + feature.properties.tor_f_scal + "<br>" + 
         "Deaths: " + feature.properties.deaths_dir + "<br>" + 
         "Damage: " + feature.properties.damage_pro + ''); 
       } 
      }).addTo(efuSub); 
     }); 
     $.getJSON(sqlF1, function(cartodbdata) { 
      geojsonlayer= L.geoJson(cartodbdata, { 
       onEachFeature: function (feature, layer) { 
        layer.bindPopup("F-Scale: " + feature.properties.tor_f_scal + "<br>" + 
         "Deaths: " + feature.properties.deaths_dir + "<br>" + 
         "Damage: " + feature.properties.damage_pro + ''); 
       } 
      }).addTo(f1Sub); 
     }); 

     $.getJSON(sqlF2, function(cartodbdata) { 
      geojsonlayer= L.geoJson(cartodbdata, { 
       onEachFeature: function (feature, layer) { 
        layer.bindPopup("F-Scale: " + feature.properties.tor_f_scal + "<br>" + 
         "Deaths: " + feature.properties.deaths_dir + "<br>" + 
         "Damage: " + feature.properties.damage_pro + ''); 
       } 
      }).addTo(f2Sub); 
     }); 

     //Tornado Path. 
     var tPath = new L.layerGroup([]); 
     var path = "https://kmitch24.carto.com/api/v2/sql?format=GeoJSON&q=SELECT * FROM tornadodata_path"; 

     $.getJSON(path, function(cartodbdata) { 
      geojsonlayer= L.geoJson(cartodbdata, { 
      }).addTo(tPath); 
     }); 

     var overLayMap = { 
      "EFU": efuSub, 
      "F1": f1Sub, 
      "F2": f2Sub, 
      "Tornado Path": tPath 
     }; 

     L.control.layers(baseMaps, overLayMap).addTo(map); 
    </script> 
</body> 
</html> 
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script> 

답변

0

편집 : 당신은 단순히 Leaflet.markercluster 버전 1.0.0 대신 1.0.0-beta.2.0 같은 가장 최근의

버전 1.0.0 (사용해야 같은

이 보이는 쓰기)에는 PR#624이 포함되어 있으므로 중첩 된 레이어 그룹을 관리 할 수 ​​있습니다.

데모 : http://plnkr.co/edit/iA9f3jd5Ov0Ln46Id7FW?p=preview


원래 답 : -S

Leaflet.FeatureGroup.SubGroup가 예상되는 : 하위 그룹이 상위 마커 클러스터 그룹에서 ... "분리"될 것으로 보인다 이유를 정확하게

확실하지 않음 jQuery AJAX에 의해 검색된 GeoJSON 데이터에서 리플릿 레이어를 만드는 데 사용되는 GeoJSON 그룹과 마찬가지로 중간 계층 그룹과 올바르게 작업 할 수 있습니다. 데모 :

$.getJSON(sqlEFU, function(cartodbdata) { 
    geojsonlayer= L.geoJson(cartodbdata, { 
     onEachFeature: function (feature, layer) { 
      layer.bindPopup("some text").addTo(efuSub); // Add individual layers 
     } 
    })/*.addTo(efuSub)*/; // No longer add the intermediate GeoJSON group. 
}); 

데모 : http://plnkr.co/edit/eMzG9RkoV7fsNODotiSt?p=preview

것은 당신이 최소한의 테스트를 구축 할 수있는 경우 http://plnkr.co/edit/Y0OlNGdBE47AnsL4seLK?p=preview

해결 방법은 중간 그룹 없애 적절한 하위 그룹에 직접 개별 레이어를 추가하는 것입니다 문제가 재현되는 경우 코드가 작동하지 않는 이유를 쉽게 찾아 낼 수 있습니다.

+0

코드를 살펴 줘서 고맙습니다. 제공 한 데모를 검토 한 결과, 필요한 모든 추가 기능을 모두 수정하고 작동시킬 수있었습니다. – osupb37