我从服务器加载功能并将它们添加为矢量图层的源。
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
了。我该如何防止这种情况?
答案 0 :(得分:1)
我终于让它现在运行了。
问题是,从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
}),
...
})