[英]Force refresh openlayers 5.3 cluster source with bbox strategy
I am using OpenLayers for our javascript map application. 我正在将OpenLayers用于我们的javascript地图应用程序。 There is a cluster layer which has custom loader function (that means the data are loaded through this function from database) and the behavior is set to bbox , which means that the layer is refreshed everytime the user moves the map.
有一个具有自定义加载器功能的群集层(这意味着数据是通过此功能从数据库加载的),并且行为设置为bbox ,这意味着该层在用户每次移动地图时都会刷新。
let vectorSource = new sourceVector({
strategy: bbox,
loader: function(extent, resolution, projection) {
Log.warn("refresh attempt");
// other long code that is not important and works well
}
});
let clusterSource = new Cluster({
distance: 25,
source: vectorSource
});
However, I am in a situation that the underlaying data gets changed by the user and I need to refresh the map manually. 但是,我处于底层数据被用户更改的情况,我需要手动刷新地图。
I tried a lot of approaches that I found, for example: 我尝试了很多发现的方法,例如:
clusterSource.refresh(); // does nothing
layer.set('visible', false);
layer.set('visible', true); // does nothing
map.removeLayer(layer);
map.addLayer(layer); // does nothing
clusterSource.clear(true); // removes all items from the map but does not call loader function to load them again
clusterSource.loadedExtentsRtree_.clear(); // does nothing
So far, the only thing that worked is to move the map elsewhere and then return back: 到目前为止,唯一有效的方法是将地图移到其他位置然后返回:
let originalCenter = [
map.getView().getCenter()[0],
map.getView().getCenter()[1]
];
let newCenter = [
map.getView().getCenter()[0] + 50,
map.getView().getCenter()[1] + 50
];
setTimeout(() => {
map.getView().animate({
center: newCenter,
duration: 0
});
setTimeout(() => {
map.getView().animate({
center: originalCenter,
duration: 0
});
}, 10);
}, 10);
But that of course causes the map to flicker there and back and I would like to avoid that. 但这当然会使地图在那儿来回闪烁 ,我想避免这种情况。 Is there some hidden solution for this problem that actually works?
对于这个实际可行的问题,有一些隐藏的解决方案吗? I wanted simply to force the cluster layer to call the defined loader function and I didn't expect it to be such a problem.
我只想强制集群层调用已定义的加载器函数 ,但我并不认为这会成为问题。
Thanks for any tips, Vojtech 感谢您的任何提示,Vojtech
Thanks to Mike's suggestion, I found the solution. 感谢迈克的建议,我找到了解决方案。
The solution is following (the key is to use the inner vectorSource of cluster source): 解决方案如下(关键是使用群集源的内部vectorSource):
clusterSource.getSource().clear();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.