Skip to main content
2 of 3
edited body
Mike
  • 12.9k
  • 1
  • 12
  • 18

I presume your view is in a diferent projection because your code works when the view is in EPSG:4326. When the view projection differs from the WFS you will need to transform the bbox extent for the WFS request from view projection to EPSG:4326, then transform the features from EPSG:4326 to view projection when reading them.

var sourceWFS = new ol.source.Vector({
    loader: function (extent, resolution, projection) {
        var extent4326 = ol.proj.transformExtent(extent, projection, 'EPSG:4326');
        $.ajax('http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/wfs/Bacini_idrografici.map', {
            type: 'GET',
            data: {
                service: 'WFS',
                version: '1.1.0',
                request: 'GetFeature',
                typename: 'ID.ACQUEFISICHE.BACINIIDROGRAFICI.PRINCIPALI',
                srsname: 'EPSG:4326',
                bbox: extent4326.join(',') + ',EPSG:4326'
            }
        }).done(loadFeatures)
        .fail(function () {
        alert("error loading vector layer");
        });
        function loadFeatures(response) {
            var formatWFS = new ol.format.WFS();
            sourceWFS.addFeatures(formatWFS.readFeatures(response, {
                dataProjection: 'EPSG:4326',
                featureProjection: projection
            }));
        }
    },
    strategy: ol.loadingstrategy.bbox,
});

var vectorLayerWFS = new ol.layer.Vector({
  source: sourceWFS,
});
Mike
  • 12.9k
  • 1
  • 12
  • 18