3

Using openlayers 5, I'm getting an issue where geometry persists on the map after deleting it - seems like the map isn't updating properly. Is there a way to force an update / refresh of the map and its features? Can't find anything related to that in the documentation.

Tried using map.render() but that doesn't seem to work either.

Calling .getFeatures() on my VectorSource object shows that the geometry/object is not in the features list, but it still appears on the map on my page.

Ben
  • 62
  • 1
  • 8

2 Answers2

8

[EDIT] As noted in the OpenLayers changelog v6.0.0 you have now to call:

layer.changed()

Try to refresh your source:

layer.getSource().refresh();

Or do this for all layers:

map.getLayers().forEach(layer => layer.getSource().refresh());

If this still does not work:

Assuming your layer is the first layer, try console.log(map.getLayers()[0].getFeatures());. Are the deleted features logged? If so, it might be a reference problem.

goliatone
  • 2,183
  • 16
  • 29
pzaenger
  • 11,381
  • 3
  • 45
  • 46
  • What I'm actually doing is removing a layer from my map, but it's still showing on the map. If I look on the console and call map.getLayers, it shows that the layer is no longer inside the map but I'm still seeing it. – Ben Feb 15 '19 at 17:05
1

I appreciate this is an old question. If you're using TypeScript:

import Vector from "ol/layer/Vector";

map?.getAllLayers().forEach((layer) => {
      if (layer instanceof Vector) {
        layer.getSource()?.refresh();
      }
  });