簡體   English   中英

OpenLayers。 如何刷新集群?

[英]OpenLayers. How to refresh cluster?

我動態地向集群添加了功能,但據我所知,集群無法正常工作。 我的圖層定義如下:

var source = new ol.source.Vector({});

var cluster = new ol.source.Cluster({
    distance: 10,
    source: source
});

var style = new ol.style.Style({
      fill: new ol.style.Fill({
            color: "rgba(255,150,0,1)"
      }),
      stroke: new ol.style.Stroke({
          color: "rgba(255,150,0,1)",
          width: 1
      }),
      image: new ol.style.Circle({
           radius: 1,
           fill: new ol.style.Fill({
               color: "rgba(255,150,0,1)"
           })
       }),
       zIndex: 1
   });

var layer = new ol.layer.Vector({
   source: cluster,
   style: style,
   zIndex: 1
});

我在其中一個函數中批量添加了多個要素(點幾何),這些要素將圖層作為參數。 它是這樣的:

layer.getSource().clear();  
layer.setVisible(true); 
layer.getSource().addFeatures(features); // features is a large array of features

但是,如果放大和縮小,則會看到以下圖片:

在此處輸入圖片說明

和:

在此處輸入圖片說明

在第二個屏幕快照中,您可以看到我的圖層顯示了所有要素,並且忽略了聚類參數。 為什么會這樣,我該如何解決? (PS。如果有關系,我正在使用最新版的OL)

一切正常,我用您的代碼更新了此示例,並添加了具有隨機值的功能openlayers.org/en/latest/examples/cluster.html

<!DOCTYPE html>
<html>
  <head>
    <title>Clustered Features</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <form>
      <label>cluster distance</label>
      <input id="distance" type="range" min="0" max="100" step="1" value="40"/>
    </form>
    <script>
      var distance = document.getElementById('distance');
        var source = new ol.source.Vector({
              });

        var cluster = new ol.source.Cluster({
            distance: 10,
            source: source
        });

        var style = new ol.style.Style({
            fill: new ol.style.Fill({
                color: "rgba(255,150,0,1)"
            }),
            stroke: new ol.style.Stroke({
                color: "rgba(255,150,0,1)",
                width: 1
            }),
            image: new ol.style.Circle({
                radius: 1,
                    fill: new ol.style.Fill({
                        color: "rgba(255,150,0,1)"
                    })
                }),
                zIndex: 1
           });

        var clusters = new ol.layer.Vector({
           source: cluster,
           style: style,
           zIndex: 1
        });


    var map = new ol.Map({
        layers: [clusters],
        target: 'map',
        view: new ol.View({
            center: [0, 0],
            zoom: 2
        })
    });

    var count = 20000;
    var features = new Array(count);
    var e = 4500000;
    for (var i = 0; i < count; ++i) {
        var coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e];
        /*features[i] =*/ source.addFeature(new ol.Feature(new ol.geom.Point(coordinates)));
    }

    distance.addEventListener('input', function() {
        cluster.setDistance(parseInt(distance.value, 10));
    });
    </script>
  </body>
</html>

祝好運

暫無
暫無

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

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