I'm loading Geojson-data (from Postgis) to Openlayers 3 via bounding box stategy. Everything looks good.
The problem ist, that the data is reloaded all the time even if no change of the boundig box occurs.
Here is my code:
var geoJsonFormat = new ol.format.GeoJSON();
var vectorSource = null;
function loaderFunction(extent, resolution, projection) {
var extent = ol.proj.transformExtent(extent, projection.getCode(), ol.proj.get('EPSG:4326').getCode());
var url = "./_mapdata/n2k_dh_geojson.php?bbox=" + extent.join(',');
$.ajax({
url: url,
success: function (data) {
var features = geoJsonFormat.readFeatures(data, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
vectorSource.clear(true);
vectorSource.addFeatures(features);
}
});
}
vectorSource = new ol.source.Vector({
format: geoJsonFormat,
loader: loaderFunction,
projection: 'EPSG:4326',
strategy: ol.loadingstrategy.bbox
});
…. later I add the layer to a layers array
new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
fill: new ol.style.Fill({color:'rgba(100,250,0,0.1)'}),
stroke: new ol.style.Stroke({
color: 'green',
width: 2
})
})
})
How can I prevent, that the data is reloaded all the time?
Best Answer
You should not call
vectorSource.clear(true)
in your loader function. See this Issue