1

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?

asked Oct 14, 2016 at 20:12

1 Answer 1

0

You should not call vectorSource.clear(true) in your loader function. See this Issue

answered Oct 15, 2016 at 11:40
1
  • 1
    OK, as mentioned in the link You indicated, after removing the vectorSource.clear(true) I had the problem, that the data was multiplied after each movement of the map. The solution was to change the query that delivered the Geojson from my Postgis-database. I just had to return my gid-field as id-field, so that Openlayers recognized all the features with an id as unique. Thanks for Your help. Commented Oct 15, 2016 at 13:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.