2

有没有办法将通过 .getCenter() 在 onEachFeature 事件(见下文)中创建的中心点添加到 L.Marker 或类似对象,该对象包含在该事件上创建的所有中心点,Leaflet 可以使用。标记集群?

我认为使用 featureGroup 可能是解决方案,但显然不是。

我可以通过 L.Marker 或 L.FeatureGroup 上的 addTo(map) 方法在地图上显示未聚集的中心点,但不幸的是,当我尝试在这两个创建的对象上使用 markerCluster 时,地图会出现空的。浏览器的控制台上没有出现任何错误消息。

我对 JS 还是很陌生,所以我有一种预感,我缺少一些基本的东西,也许是关于 L.Markercluster 本身,我对这里的任何新手错误表示歉意。

图书馆:

<!-- Leaflet -->

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css"
  integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ=="
  crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"
  integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log=="
  crossorigin=""></script>

<!-- ESRI Leaflet -->

<script src="https://unpkg.com/esri-leaflet@2.0.4/dist/esri-leaflet.js"></script>

<!-- Leaflet-markercluster -->

<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.0.6/dist/MarkerCluster.css"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.0.6/dist/MarkerCluster.Default.css"></script>
<script src="https://unpkg.com/leaflet.markercluster@1.0.6/dist/leaflet.markercluster.js"></script>

<!--  Leaflet.MarkerCluster.LayerSupport -->

<script src="https://unpkg.com/leaflet.markercluster.layersupport@1.0.5/dist/leaflet.markercluster.layersupport.js"></script>

脚本:

<script>

  var map = L.map('map', {
    center: [42.389810, -72.524684],
    zoom: 5
  });

  var esriTopo = L.esri.basemapLayer('Topographic').addTo(map);

  var ProjectMap = L.esri.featureLayer ({
    url: 'https://services.arcgis.com/2gdL2gxYNFY2TOUb/arcgis/rest/services/NECSC_Test_Data/FeatureServer/1',
    //cheap hack to making the polygons invisible
    weight: 0,
    fillOpacity: 0,

    // creating the centerpoints
    onEachFeature: function(feature,layer){
      if (feature.geometry.type = 'Polygon') {
        var bounds = layer.getBounds();
        var center = bounds.getCenter();

        var centerpoints = L.marker(center);
        centerpointlayer.addLayer(centerpoints);
        // centerpointlayer defined below as global variable
      };
    };
  }).addTo(map);

  var centerpointlayer = L.featureGroup();
  // 

  var clusters = L.markerClusterGroup.layerSupport();
  clusters.addTo(map);
  clusters.checkIn(centerpointlayer);

  map.zoomIn(5);
  map.zoomOut(5);

</script>

</body>
</html>
4

1 回答 1

3

嘎...原来我在实现 L.Markercluster 错误(即,不像 API 文档中所说的那样)。最后 /script 之前的最后几行代码应为:

var centerpointlayer = L.layerGroup();
var clusters = L.markerClusterGroup.layerSupport();
clusters.addLayer(centerpointlayer);
map.addLayer(clusters);
于 2017-08-11T04:43:37.207 回答