1

I have an OpenLayers 3 app where the server updates a GeoJSON file for my app every so often with new features. My problem is that it seems to use a cached file instead of using the new one. Lets start with the caching problem. When using:

var locationSource = new ol.source.Vector({
     url: '/openlayers/location.geojson',
     format: new ol.format.GeoJSON({
       defaultDataProjection :'EPSG:4326' 
     })
  });

The data shows on the map but when the server generates a new location.geojson file and a timer I have that fires a function that does:

locationSource.clear();
locationSource.refresh(); 

It ends up using a cached version most of the time so new or removed items in location.geojson file are not seen on the map. I used firebug clicking on the Network/XHR tab to view geojson file transactions. I would like the map to update when a new file is available on the server.

After poking around seeing what others did to get around this problem, I changed my source.Vector to remove url property and add a loader property. See locationSource var in the source below. In general, adding time to the request url for location.geojson (/openlayers/location.geojson?t=1474651367500) it forces a call to retrieve the latest version of this file. Using firebug I can see that it does retrieve the location.geojson file correctly and adds the new features using source.addFeatures(features); I know that the GeoJSON file is good because it displays fine using the locationSource var above which had the caching issue and uses the url property. I also checked in firebug and can see that the response from the server has valid JSON.

The problem is that after source.addFeatures(features); does it thing, I see nothing on the map. Tried adding the following just after addFeatures and still didn't see anything on the map. What am I missing here???

Update #1 It appears that the style function is not being called after the data is loaded using source.addFeatures(features); Not sure why??:

var locvectorLayer = new ol.layer.Vector({
    source: LOCclusterSource,
    style: styleFunction2('green',8,2,10),
    minResolution: 0,
    maxResolution: 15
});

Also, tried using the following after source.addFeatures(features); which did nothing...

map.render();
source.changed();
source.refresh();

I am using a cluster map layer too:

var LOCclusterSource = new ol.source.Cluster({
    distance: 5,
    source: locationSource
});
var locvectorLayer = new ol.layer.Vector({
    source: LOCclusterSource,
    style: styleFunction2('green',8,2,10),
    minResolution: 0,
    maxResolution: 15
});

Update #2 - Problem solved, turns out I was using the wrong map projection. Anyhow, since this place is so quiet, I posted my problem over on stackoverflow (stackoverflow question and answer)

Source code:

    var utils = {
    refreshGeoJson: function(source,url) {
      var now = Date.now();
          //var url = source.getUrl();
      url += '?t=' + now;
      console.info(url);
          this.getJson(url).when({
        ready: function(response) {
          var format = new ol.format.GeoJSON();
          var features = format.readFeatures(response, {
          featureProjection: 'EPSG:4326'
          });
          console.info(features);
          source.addFeatures(features);
        }
         });
},
    getJson: function(url) {
          var xhr = new XMLHttpRequest(),
          when = {},
          onload = function() {
            if (xhr.status === 200) {
                  when.ready.call(undefined, JSON.parse(xhr.response));
            }
          },
          onerror = function() {
            console.info('Cannot XHR ' + JSON.stringify(url));
          };
          xhr.open('GET', url, true);
          xhr.setRequestHeader('cache-control', 'no-store');
          xhr.onload = onload;
          xhr.onerror = onerror;
          xhr.send(null);
          return {
        when: function(obj) { when.ready = obj.ready; }
        };
    }
};
var vectorLoader = function(extent, resolution, projection) {
  utils.refreshGeoJson(locationSource,'/openlayers/location.geojson');
}
var locationSource = new ol.source.Vector({
     format: new ol.format.GeoJSON({
        defaultDataProjection :'EPSG:4326' 
      }),
      loader: vectorLoader
});

closed as off-topic by nmtoken, aldo_tapia, whyzar, xunilk, Andre Silva Jan 5 '18 at 13:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – nmtoken, aldo_tapia, whyzar, xunilk, Andre Silva
If this question can be reworded to fit the rules in the help center, please edit the question.