OpenLayers.如何刷新集群? [英] OpenLayers. How to refresh cluster?

查看:87
本文介绍了OpenLayers.如何刷新集群?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态地向集群添加了功能,但据我所知,集群不起作用.我的图层定义如下:

I dynamically add features to my cluster, however, as far as I can see clustering does not work. My layer is defined like so:

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
});

然后我在其中一个函数中批量添加了多个要素(点几何),这些要素将图层作为参数.它是这样的:

And I add multiple features (Point geometries) in bulk in one of my functions, which take the layer as an argument. It does it like so:

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

但是,如果放大和缩小,我会看到以下图片:

However, if I zoom in and zoom out, I see these pictures:

和:

在第二个屏幕截图中,您可以看到我的图层显示了所有要素,并且忽略了聚类参数.为什么会这样,我该如何解决? (PS.如果有关系,我正在使用最新版的OL)

On the second screen shot, you can see, that my layer shows all features and ignores clustering parameter. Why is that and how can I fix it? (PS. If it matters, I'm using the latest version of OL)

推荐答案

这很好,我用您的代码更新了此示例,并添加了具有随机值的功能 openlayers.org/en/latest/examples/cluster.html

this is working fine, i updates this example with your code and added features with random values 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>

祝你好运

这篇关于OpenLayers.如何刷新集群?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆