简体   繁体   English

OpenLayers-3-source.clear()的正确用法是什么

[英]OpenLayers-3 - What is the correct usage of source.clear()

I have a ol 3.10.1 map where the goal is to redraw the features of a layer dynamically. 我有一个ol 3.10.1地图,目标是动态重绘图层的功能。 On the road to get there, I'm using the source.clear() function. 在到达那里的路上,我正在使用source.clear()函数。 The strange thing is that the source.clear() actually clear the features from the layer at the current zoom level, but while zooming in or out the features are still there. 奇怪的是,source.clear()实际上在当前缩放级别上清除了图层中的要素,但是在放大或缩小要素时仍然存在。 Am I using the source.clear() function the correct way? 我是否以正确的方式使用source.clear()函数? Please find bellow the code snippet which I'm using for testing purposes. 请在下面找到我用于测试目的的代码段。

        var image = new ol.style.Circle({
          radius: 5,
          fill: null,
          stroke: new ol.style.Stroke({color: 'red', width: 1})
        });

        var styles = {
          'Point': [new ol.style.Style({
            image: image
          })]};

        var styleFunction = function(feature, resolution) {
          return styles[feature.getGeometry().getType()];

        };  

        var CITYClusterSource = new ol.source.Cluster({
                source: new ol.source.Vector({
               url: 'world_cities.json',
                format: new ol.format.GeoJSON(),
                projection: 'EPSG:3857'
            }),
        })
        var CITYClusterLayer = new ol.layer.Vector({
            source: CITYClusterSource,
            style: styleFunction

        });

        setTimeout(function () { CITYClusterSource.clear(); }, 5000);

        var map = new ol.Map({
            target: 'map',
            renderer: 'canvas',
              layers: [
                new ol.layer.Tile({
                  source: new ol.source.OSM(),
                }),
                CITYClusterLayer
            ],
            view: new ol.View({
                center: ol.proj.transform([15.0, 45.0], 'EPSG:4326', 'EPSG:3857'),
                zoom:3
            })
        });

I'm using the setTimout() function to have the features visible for some seconds, before they are supposed to be cleared. 我正在使用setTimout()函数使功能可见几秒钟,然后再将其清除。

Please advice. 请指教。

UPDATE : http://jsfiddle.net/jonataswalker/ayewaz87/ 更新http : //jsfiddle.net/jonataswalker/ayewaz87/

I forgot, OL will load your features again and again, for each resolution. 我忘了,OL会针对每种分辨率一次又一次地加载您的功能。 So if you want to remove once and for all, use a custom loader, see fiddle. 因此,如果您要一劳永逸地删除,请使用自定义加载程序,请参阅小提琴。

Your source is asynchronously loaded, so set a timeout when it's ready: 您的源是异步加载的,因此请在准备好后设置超时:

CITYClusterSource.once('change', function(evt){
    if (CITYClusterSource.getState() === 'ready') {
        // now the source is fully loaded
        setTimeout(function () { CITYClusterSource.clear(); }, 5000);
    }
});

Note the once method, you can use on instead of it. 请注意once方法,您可以使用on代替它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM