1

I am using Openlayers 3.0 , I have created a vector layer where the source for the vector layer is a server vector. Now I want to update this layer on checkbox click.So I created the function below however , no changes are dispatched. Can someone help please ?

function update(layerName , layerSource) {
               var formatWFS = new ol.format.WFS({
                    featureNS: featureNS,
                    featureType: layerName
                });
                $.ajax({
                    url: layerURL,
                    dataType: 'xml',
                    jsonp: false,
                    jsonpCallback: 'getJson',
                    type: 'POST',
                    async: false,
                    success: function(data){
                        layerSource.clear();
                        layerSource.loadedExtents_.clear();
                        var features = formatWFS.readFeatures(data);
                        layerSource.addFeatures(features);
                        layerSource.dispatchChangeEvent(); 
                    }
                });
    }

This is the solution I have found for my problem stated above :

function update (layerName , layerSource) {
       var staticVector = new ol.source.StaticVector({
                format: new ol.format.WFS({
                    featureNS: featureNS,
                    featureType: layerName
                }),
                projection: layerProjection
            });
       $.ajax({
                url: url,
                dataType: 'xml',
                jsonp: false,
                jsonpCallback: 'getJson',
                type: 'POST',
                async: true,
                success: function(data){
                    layerSource.clear();
                    layerSource.addFeatures(staticVector.readFeatures(data));
                    layerSource.dispatchChangeEvent(); 
                }
            });
}

I had to use the solution above and not use the layer source as source.Vector for example as the projection was not correct otherwise. Please note that the solution listed is for Openlayers 3.0.0

4
  • see gis.stackexchange.com/questions/89896/…
    – Mapperz
    Commented Sep 10, 2015 at 15:11
  • I don't get how this would help me solve my issue. Can you please explain ? Also , for my problem the data is loaded correctly within the layerSource however once the success function of the ajax call is done the source layer returns to previous state.
    – girlintech
    Commented Sep 10, 2015 at 15:17
  • There is a lack of informations, try to provide a jsfiddle.
    – oterral
    Commented Sep 14, 2015 at 9:24
  • You should post the solution as an answer, not as an edit to the question. Commented May 1, 2016 at 11:57

1 Answer 1

1

I was looking for a similar solution and the only way to do this update was a very ugly piece of code with removing the old layer and adding it again. See below:

map.removeLayer(layerName);

layerNameSource = new ol.source.Vector({...});

layerName = new ol.layer.Vector({
    source: layerNameSource
    ....
});

map.addLayer(layerName);
3
  • Thank you so much for your reply. :) I have actually managed to solve the problem by changing my source vector at creation to StaticVector instead of ServerVector , as I was not able to customize my loader function for the servervector to refresh when I needed it to.
    – girlintech
    Commented Sep 17, 2015 at 13:14
  • I'm glad if I could help. I would like you to ask to edit your original post and share your solution with others. Thx
    – fidelfisch
    Commented Sep 18, 2015 at 7:14
  • Thanks for pointing this out. I have added my solution as an edit now :)
    – girlintech
    Commented Sep 20, 2015 at 9:02

Your Answer

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

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