未显示的功能

Features not showing up

我正在从服务器加载要素并将它们添加为矢量图层的源。

var map = new ol.Map({
    target: 'map',
    interactions: ol.interaction.defaults().extend([
        new ol.interaction.DragRotate()
    ]),
    controls: ol.control.defaults().extend([
        new ol.control.FullScreen(),
        new ol.control.ScaleLine()
    ]),
    layers: [
        new ol.layer.Tile({
            source: new ol.source.OSM()
        }),
        new ol.layer.Tile({
            source: new ol.source.TileWMS({
                url: 'https://server.com/wms?',
                params: {
                    'LAYERS': 'operational_structures',
                    'TILED': true
                }
            }),
            maxResolution: 500,
            opacity: 0.3
        }),
        new ol.layer.Vector({
            source: new ol.source.Vector({
                url: function (extent, resolution, projection) {
                    return 'https://server/wms?Request=GetFeature&BBOX='
                        + extent.join(',');
                },
                format: new ol.format.GeoJSON(),
                strategy: ol.loadingstrategy.bbox
            }),
            style: new ol.style.Style({
                image: new ol.style.Circle({
                    radius: 10,
                    fill: new ol.style.Fill({color: 'rgba(255, 0, 0, 0.1)'}),
                    stroke: new ol.style.Stroke({color: 'red', width: 1})
                })
            })
        })
    ],
    view: new ol.View({
        center: ol.proj.fromLonLat([0, 0]),
        zoom: 4
    })
});

获取所有功能一切正常。我可以使用 layer.getSource().getFeatures() 访问它们。我的数据如下所示:

{
    "type": "FeatureCollection",
    "features": [
    {
        "type": "Feature",
        "properties": {
            "id": 1,
            "ref": "GIS_af05"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [ -119383.2138463442, 7156374.7828825945 ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "id": 2,
            "ref": "GIS_af06" },
        "geometry": {
            "type": "Point",
            "coordinates": [ -117573.06816312684, 7163838.359699009 ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "id": 3,
            "ref": "GIS_af21" },
        "geometry": {
            "type": "Point",
            "coordinates": [ -128431.22137966838, 7169061.1280527115 ]
        }
    }]
}

但由于未知原因,他们不会出现。我错过了什么吗?


编辑:

所以我发现它弄乱了预测。它尝试将要素坐标从 ESPG:4326 转换为 ESPG:3857。问题是,他们已经 ESPG:3857。我怎样才能防止这种情况发生?

现在终于明白了运行

问题是,从 GIS 服务器返回的数据没有指定其投影。所以Openlayers3默认使用EPSG:4326

经过长时间的文档搜索,我发现,我可以在 ol.format.GeoJSON 中设置默认投影(没想到那里有)。

解决方案如下所示:

new ol.layer.Vector({
    source: new ol.source.Vector({
        url: function (extent, resolution, projection) {
            return 'https://server/wms?Request=GetFeature&BBOX='
                + extent.join(',');
        },
        format: new ol.format.GeoJSON({
            defaultDataProjection: 'EPSG:3857' // added line
        }),
        strategy: ol.loadingstrategy.bbox
    }),
    ...
})