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
-
see gis.stackexchange.com/questions/89896/…Mapperz– Mapperz ♦2015年09月10日 15:11:33 +00:00Commented 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– girlintech2015年09月10日 15:17:14 +00:00Commented Sep 10, 2015 at 15:17
-
There is a lack of informations, try to provide a jsfiddle.oterral– oterral2015年09月14日 09:24:26 +00:00Commented Sep 14, 2015 at 9:24
-
You should post the solution as an answer, not as an edit to the question.Andre Silva– Andre Silva2016年05月01日 11:57:55 +00:00Commented May 1, 2016 at 11:57
1 Answer 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);
-
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– girlintech2015年09月17日 13:14:36 +00:00Commented 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. Thxfidelfisch– fidelfisch2015年09月18日 07:14:48 +00:00Commented Sep 18, 2015 at 7:14
-
Thanks for pointing this out. I have added my solution as an edit now :)girlintech– girlintech2015年09月20日 09:02:18 +00:00Commented Sep 20, 2015 at 9:02