【问题标题】:OpenLayers. How to refresh cluster?开放层。如何刷新集群?
【发布时间】:2018-06-21 11:17:06
【问题描述】:

我动态地向我的集群添加功能,但是,据我所知,集群不起作用。我的层是这样定义的:

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)

【问题讨论】:

  • 设置更大的距离,比如 50
  • @nalm。如果我设置更高的距离,我只会看到一个橙色的矩形,点之间没有任何空格。
  • 那是因为它们现在正在聚集!使用您的特征数组的大小在您的样式上设置一个文本,应该是您正在寻找的!看到这个openlayers.org/en/latest/examples/cluster.html
  • 好吧,看起来它们根本没有聚集。如果我设置更高的距离,我希望看到更少的特征(许多特征应该折叠,因为它们之间的可见距离太短)。但是特征的数量越来越多。如果我从集群源切换到通用矢量源,我看不出有什么区别。

标签: javascript openlayers openlayers-3


【解决方案1】:

这很好,我用你的代码更新了这个例子,并添加了随机值的特性 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>

祝你好运

【讨论】:

  • 顺便说一句。你为什么评论features[i] =?如果使用 addFeatures 批量添加功能,它会起作用吗?
  • @nalm 可以在图像中添加集群的大小吗?提前致谢。
  • i 认为你可以设置一个样式来集群图层,这样你就可以添加一个图像、多边形和许多其他的东西
猜你喜欢
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 2010-11-15
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多