我试图通过查询提供GML数据的公共服务WFS服务器来为我的OpenLayers地图添加功能。
// initalize the map
var map = new ol.Map({
layers: [
new ol.layer.Tile({
// OpenLayers public map server
source: new ol.source.OSM()
}),
],
target: 'map',
view: new ol.View({
// center on Murica
center: [-10997148, 4569099],
zoom: 4
})
});
var xmlhttp = new XMLHttpRequest();
// execute this once the remote GML xml document has loaded
xmlhttp.onload = function() {
console.log("GML XML document retrieved. executing onload handler:");
var format = new ol.format.GML3();
var xmlDoc = xmlhttp.responseXML;
console.log("you will see multiple features in the xml: ");
console.log(xmlDoc);
// Read and parse all features in XML document
var features = format.readFeatures(xmlDoc, {
featureProjection: 'EPSG:4326',
dataProjection: 'EPSG:3857'
});
console.log("for some reason only a single feature will have been added: ")
console.log(features);
console.log("Why is this?");
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
format: format
})
});
// Add features to the layer's source
vector.getSource().addFeatures(features);
map.addLayer(vector);
};
// configure a GET request
xmlhttp.open("GET", "http://geoint.nrlssc.navy.mil/dnc/wfs/DNC-WORLD/feature/merged?version=1.1.0&request=GetFeature&typename=DNC_APPROACH_LIBRARY_BOUNDARIES&srsname=3857",
true);
// trigger the GET request
xmlhttp.send();
这是一个带有错误的CodePen。
http://codepen.io/anon/pen/yamOEK
在这里,您可以将其打包成单个HTML文件: https://drive.google.com/open?id=0B6L3fhx8G3H_cmp1d3hHOXNKNHM
我可以使用有效的typename将包含多个功能的整个功能集合成功下载到我的变量xmlDoc中。但是,当我使用format.ReadFeatures(xmlDoc)时,OpenLayers GML格式解析器似乎只是从特征集合中提取单个特征,而它应该提取更多。
如果有人可以看看他们是否可以弄清楚我是否做了一些愚蠢的错误或者它是OpenLayers3中的合法错误,那将是非常好的。非常感谢能够提供帮助的任何人!
答案 0 :(得分:1)
添加了单个功能,因为读取整个文档而不是format.readFeatures(xmlDoc)解析每个功能。这是源代码:
var vector;
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
],
target: 'map',
view: new ol.View({
center: [-8197020.761224195,8244563.818176944],
zoom: 4
})
});
var xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
var format = new ol.format.GML3();
var xmlDoc = xmlhttp.responseXML;
vector = new ol.layer.Vector({
source: new ol.source.Vector({
format: format
})
});
for (var i = 1; i < xmlDoc.children[0].children.length; i++) {
var features = format.readFeatures(xmlDoc.children[0].children[i], {
featureProjection: 'EPSG:4326'
});
features.getGeometry().transform('EPSG:4326', 'EPSG:3857');
vector.getSource().addFeature(features);
}
map.addLayer(vector);
map.getView().fit(vector.getSource().getExtent(), map.getSize())
};
xmlhttp.open("GET", "http://geoint.nrlssc.navy.mil/dnc/wfs/DNC-WORLD/feature/merged?version=1.1.0&request=GetFeature&typename=DNC_APPROACH_LIBRARY_BOUNDARIES&srsname=3857",
true);
// trigger the GET request
xmlhttp.send();
这是CodePen结果。 http://codepen.io/anon/pen/bwXrwJ