我有一个ol3.10.1映射,目标是动态地重新绘制一个层的特性。在实现这个目标的路上,我使用了source.clear()函数。奇怪的是,source.clear()实际上在当前缩放级别上清除了图层中的特征,但是当缩放时,这些特性仍然存在。我是否以正确的方式使用source.clear()函数?请找到下面的代码片段,我正在使用的测试目的。
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()函数,在这些特性被清除之前,让它们在几秒钟内都是可见的。
请指点。
发布于 2015-10-22 08:38:22
更新:http://jsfiddle.net/jonataswalker/ayewaz87/
我忘了,OL会一次又一次地加载您的功能,用于每个分辨率。因此,如果您想一劳永逸地删除,请使用自定义加载程序,请参阅小提琴。
您的源是异步加载的,所以在它准备好时设置一个超时:
CITYClusterSource.once('change', function(evt){
if (CITYClusterSource.getState() === 'ready') {
// now the source is fully loaded
setTimeout(function () { CITYClusterSource.clear(); }, 5000);
}
});注意once方法,您可以使用on代替它。
https://stackoverflow.com/questions/33275492
复制相似问题