简体   繁体   中英

Force refresh openlayers 5.3 cluster source with bbox strategy

I am using OpenLayers for our javascript map application. 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.

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

Thanks to Mike's suggestion, I found the solution.

The solution is following (the key is to use the inner vectorSource of cluster source):

clusterSource.getSource().clear();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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