When I load more than around 500 vector features in Openlayers, performance on tablets and telephones reduces severely. My solution has been to clear the source on the map's moveend
event. This works, but results in an annoying flash, presumably between clear and reload of the source.
An earlier solution to this "flashing" here no longer works, it results in recursive calls to the WFS.
Here is my current code:
import {GeoJSON} from 'ol/format';
import {bbox as bboxStrategy} from 'ol/loadingstrategy.js';
import VectorSource from 'ol/source/Vector.js';
import VectorLayer from 'ol/layer/Vector';
var vectorSource = new VectorSource({
format: new GeoJSON({ dataProjection: layerProjection }),
loader: function(extent, resolution, projection) {
var proj = projection.getCode();
var url = 'https://xxx.xxx/geoserver/wfs?service=WFS' +
'&version=1.1.0&request=GetFeature&typename=' + layer +
'&maxFeatures=200&outputFormat=application/json&srsname=' + proj +
'&bbox=' + extent.join(',') + ',' + proj;
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
var onError = function() {
vectorSource.removeLoadedExtent(extent);
}
xhr.onerror = onError;
xhr.onload = function() {
if (xhr.status == 200) {
//vectorSource.clear();// Causes recursion
vectorSource.addFeatures(
vectorSource.getFormat().readFeatures(xhr.responseText));
} else {
onError();
}
}
xhr.send();
},
strategy: bboxStrategy
});
var vector = new VectorLayer({
source: vectorSource,
style: style,
visible: visible
});
map.on('moveend', function(evt) {
console.log('refreshing vector');
vectorSource.clear()
});
Is there a way to avoid the clear/reload flash in Openlayers 5? Or is there another method I should be using to load vector layers (the layers I have are typically point layers with 5000 to 10000 features).
[EDIT] After comment from @ahocevar, here is the style I am using:
import {Style, Circle, Fill, Stroke} from 'ol/style';
import Defaults from './PointDefaults';
export default function() {
return new Style({
image: new Circle({
radius: Defaults.radius,
fill: new Fill({
color: 'green',
}),
stroke: new Stroke({
color: Defaults.strokeColor,
width: Defaults.strokeWidth
})
}),
});
}