user2860857
user2860857

Reputation: 559

Difference between URL and Loader on Vector Source - OpenLayers 3

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?

Upvotes: 4

Views: 6153

Answers (3)

iv67
iv67

Reputation: 4151

I just faced the same problem as yours. Method with url works well, but I faced problem when using the Loader.

Here's the problem, from your url I notice that your outputFormat=application/json, meanwhile inside the loader function you specified dataType: 'jsonp' .

The dataType: 'jsonp' only works if your outputFormat=text/javascript . Refer to this link by GeoServer , it explains format with its correct syntax.

Therefore you have to use JSONP instead of JSON, in order to enable JSONP please follow the answer in here https://gis.stackexchange.com/questions/57494/geoserver-2-3-how-to-enable-jsonp . After that change your outputFormat to outputFormat=text/javascript

Upvotes: 1

user5496249
user5496249

Reputation:

Mybe this will work better

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, resolution, projection){
                $.ajax({
                url: url,
                type:'GET',
                dataType: 'jsonp'
                }).done(function(response) {

                    var features = vectorSource.readFeatures(response, {
                        featureProjection: projection
                    });

                    vectorSource.addFeatures(features);
                });
           },
          strategy: new ol.loadingstrategy.tile(ol.tilegrid.createXYZ({maxZoom: 20}))
});

Upvotes: 2

Alexandre Dubé
Alexandre Dubé

Reputation: 2829

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.

Upvotes: 2

Related Questions