首页 > 解决方案 > 地理服务器到 openlayers wfst

问题描述

我想使用下面的代码将我的地理服务器层带入开放层但是我无法获得正确的 url 格式来输入以下层 url --- localhost:8080/geoserver/itachi/ows?service=WFS&version=1.0。 0&request=GetFeature&typeName=itachi%3Awfs_geom&maxFeatures=50

var sourceWFS = new ol.source.Vector({            
    loader: function (extent) {
        $.ajax('https://gsx.geolytix.net/geoserver/geolytix_wfs/ows', {
            type: 'GET',
            data: {
                service: 'WFS',
                version: '1.1.0',
                request: 'GetFeature',
                typename: 'wfs_geom',
                srsname: 'EPSG:3857',
                bbox: extent.join(',') + ',EPSG:3857'
            }
        }).done(function (response) {
            sourceWFS.addFeatures(formatWFS.readFeatures(response));
        });
    },

标签: javascriptopenlayers

解决方案


您也许可以使用完整的网址来做到这一点

var sourceWFS = new ol.source.Vector({
    url: 'http://localhost:8080/geoserver/itachi/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=itachi%3Awfs_geom&maxFeatures=50',
    format: formatWFS
});

但是如果您需要更改预测(例如,数据在 EPSG:4326 中返回并且视图是 EPSG:3857),您将需要使用加载器

var sourceWFS = new ol.source.Vector({
    loader: function () {
        $.ajax('http://localhost:8080/geoserver/itachi/ows', {
            type: 'GET',
            data: {
                service: 'WFS',
                version: '1.0.0',
                request: 'GetFeature',
                typename: 'itachi:wfs_geom',
                maxFeatures: '50'
            }
        }).done(function (response) {
            sourceWFS.addFeatures(formatWFS.readFeatures(response),{
                dataProjection: 'EPSG:4326',
                featureProjection: 'EPSG:3857'
            });
        });
    },
    strategy: ol.loadingstrategy.all
});

如果可以更新数据,您将需要调用sourceWFS.refresh();重新加载


推荐阅读