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?

0

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

  • 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. – geom Oct 15 '16 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.