Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用bbox策略强制刷新openlayers 5.3群集源代码_Javascript_Openlayers - Fatal编程技术网

Javascript 使用bbox策略强制刷新openlayers 5.3群集源代码

Javascript 使用bbox策略强制刷新openlayers 5.3群集源代码,javascript,openlayers,Javascript,Openlayers,我正在为我们的javascript地图应用程序使用OpenLayers。有一个集群层,该层具有自定义的加载器功能(即通过此功能从数据库加载数据),并且行为设置为bbox,这意味着每当用户移动地图时,该层都会刷新 let vectorSource = new sourceVector({ strategy: bbox, loader: function(extent, resolution, projection) { Log.warn("refresh attempt");

我正在为我们的javascript地图应用程序使用OpenLayers。有一个集群层,该层具有自定义的加载器功能(即通过此功能从数据库加载数据),并且行为设置为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
});
但是,我遇到的情况是,用户更改了参考底图数据,我需要手动刷新地图

我尝试了很多我发现的方法,例如:

clusterSource.refresh(); // does nothing
到目前为止,唯一有效的方法是将地图移到别处,然后返回:

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);
但这当然会导致地图在那里来回闪烁,我希望避免这种情况。对于这个问题,是否有一些隐藏的解决方案真正有效?我只是想强制集群层调用已定义的加载函数,我没想到会出现这样的问题


谢谢你的建议,Vojtech

谢谢迈克的建议,我找到了解决办法

解决方案如下(关键是使用集群源的内部矢量源):


clusterSource
使用
vectorSource
作为其源,bbox策略也适用于
vectorSource
,因此您需要刷新该
vectorSource.refresh()不幸的是,调用
clusterSource.getSource().refresh()
也不会触发我的加载程序函数。我只想补充一点,在OL6中,这实际上必须被clusterSource.getSource().refresh()替换。
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
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);
clusterSource.getSource().clear();