I have to load a WFS layer from my Geoserver to my website with Openlayers 3.9.0.
According to the manual there are two options to load features, loader
(ol.FeatureLoader) and url
(ol.FeatureUrlFunction).
I dont get the difference between the two. They both used to load features, loader
, does not set any URL and looks more complicated.
I tried:
var url = 'http://localhost:8080/geoserver/mapname/wfs?service=WFS&'+'version=1.0.0&request=GetFeature&typeName=mapname:awesomelayer&'+'outputFormat=application/json&maxFeatures=50'
var vectorSource = new ol.source.Vector({
format: new ol.format.GeoJSON(),
loader: function(extent){
$.ajax({
url: url,
type:'GET',
dataType: 'jsonp'
}).done(loadFeatures);
},
strategy: new ol.loadingstrategy.tile(ol.tilegrid.createXYZ({maxZoom: 20}))
});
var loadFeatures = function(response) {
var features = vectorSource.readFeatures(response);
vectorSource.addFeatures(features);
};
...and no errors at all, but also no features.
Then I simply set:
var url = 'http://localhost:8080/geoserver/mapname/wfs?service=WFS&'+'version=1.0.0&request=GetFeature&typeName=mapname:awesomelayer&'+'outputFormat=application/json&maxFeatures=50'
var vectorSource = new ol.source.Vector({
format: new ol.format.GeoJSON(),
strategy: new ol.loadingstrategy.tile(ol.tilegrid.createXYZ({maxZoom: 20})),
url: function(extent, resolution, projection){return url}
});
....which worked.
I do not get the difference, url
is similar, quicker and does not require a loadFeatures
function. I read the manual, but in practice, in terms of code, I cannot understand it. What is loader
for, why it does not set a URL and when to use it? What am I missing here?
The first reason that comes to mind: to catch errors. If your server is able to catch errors and return them inside the response, you might want to read them inside your loader and act accordingly.
A second reason would be to compute/execute some operations before those features are added to the source. Here's an example in an other thread from StackOverflow. It features both these reasons to use a loader.
If you do not need to do anything special with your data before it's being added or if you do not need to manage errors, then url
is enough for your requirements, indeed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With