首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenLayers-3 - source.clear()的正确用法是什么?

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

Stack Overflow用户
提问于 2015-10-22 07:28:37
回答 1查看 1.2K关注 0票数 1

我有一个ol3.10.1映射,目标是动态地重新绘制一个层的特性。在实现这个目标的路上,我使用了source.clear()函数。奇怪的是,source.clear()实际上在当前缩放级别上清除了图层中的特征,但是当缩放时,这些特性仍然存在。我是否以正确的方式使用source.clear()函数?请找到下面的代码片段,我正在使用的测试目的。

代码语言:javascript
复制
        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
            })
        });

我正在使用setTimout()函数,在这些特性被清除之前,让它们在几秒钟内都是可见的。

请指点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-22 08:38:22

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

我忘了,OL会一次又一次地加载您的功能,用于每个分辨率。因此,如果您想一劳永逸地删除,请使用自定义加载程序,请参阅小提琴。

您的源是异步加载的,所以在它准备好时设置一个超时:

代码语言:javascript
复制
CITYClusterSource.once('change', function(evt){
    if (CITYClusterSource.getState() === 'ready') {
        // now the source is fully loaded
        setTimeout(function () { CITYClusterSource.clear(); }, 5000);
    }
});

注意once方法,您可以使用on代替它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33275492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档