I have an OpenLayers 3.2.0 map that features some vector sources (ol.source.Vector
) and associated vector layers (ol.layer.Vector
)
When Features (ol.Feature
) are added to the vector sources, they are given a data
property that is set to the javascript object that the feature represents. TypeScript follows...
vectorSource.addFeature(new ol.Feature({
geometry: /* ... */,
data: vectorData,
}));
The vector layers then have a style-function that reads the data
property and retrieves its style:
vectorLayer = new ol.layer.Vector({
source: vectorSource,
renderBuffer: /* ... */,
style: function (feature: ol.Feature, resolution: any) {
var data = </* TypeScript Type */>feature.get('data');
if ((data) && (data.style)) {
return [data.style];
}
else {
/* return default style */
}
}
});
Sometimes, events unrelated to the map cause the styles to change. For example, when an object becomes invalid, its style changes. Clearly, since data.style
is entirely within my control, changing it is trivial.
The problem is that the map does not know that the style has changed. If I change an object's style and then zoom the map, forcing it to redraw, I notice that my style-functions run and return the new style and the feature is redrawn. How do I programatically force the map to refresh?
After some searching and experimentation, I have tried:
- Calling
render()
on theol.Map
itself. - Calling
dispatchChangeEvent()
on theol.source.Vector
- Calling
redraw()
on theol.layer.Vector
These were suggested but none of them worked, which isn't surprising since only the first method is even listed in the OpenLayers 3.2.0 API documentation and it is not marked as stable.