1

Is it possible to make a vector source load and reload features without adding/removing its parent layer to the map? I need to do that after my viewparams url variable changes.

My vectore source looks as follows

var source = new ol.source.Vector({
    url: function(extent, resolution, projection) {
        var url  = 
            'http://localhost:8080/geoserver/wfs?service=WFS' +
            '&version=2.0.0&request=GetFeature&typeNames=by:data' +
            '&outputFormat=GML3' +
            viewparams;
        return url;
    },
    format: new ol.format.WFS()
});

I've tried calling source.refresh() and source.changed() but it won't work.

  • Try source.clear() or source.clear(true) See github.com/openlayers/openlayers/issues/… – Mike Dec 13 '18 at 21:35
  • Thanks for the link @Mike. source.clear() does work but only when the layer is added to the map. Is there a way to load the source without adding the layer to the map? – yaugenka Dec 13 '18 at 22:42
  • 1
    Instead of specifying url in the source xhr or fetch the url then read and add the features from the response source.addFeatures(source.getFormat().readFeatures(xhr.response)); – Mike Dec 13 '18 at 23:06
  • Does this assume defining a custom xhr loader function based on the example from the api docs or is it possible to utilize the ol.featureloader module somehow? If you wish to post it as an answer I would mark it as resolved. Thanks. – yaugenka Dec 14 '18 at 10:07
  • No, a loader function only loads when required (so it needs to be on the map), just fetch or xhr the url and process the response. – Mike Dec 14 '18 at 10:32
1

Similar to http://openlayers.org/en/v4.6.5/examples/igc.html except using a single url

var source = new ol.source.Vector({
    format: new ol.format.WFS()
});

var url = 
    'http://localhost:8080/geoserver/wfs?service=WFS' +
    '&version=2.0.0&request=GetFeature&typeNames=by:data' +
    '&outputFormat=GML3' +
    viewparams;

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
        source.addFeatures(source.getFormat().readFeatures(xhr.response));
    }
};
xhr.open("GET", url, true);
xhr.send();

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.