我正在尝试将GML
文件加载到矢量图层并将其绘制在地图上。出于某种原因,尽管它们被解析并添加到矢量图层,但这些特征并未显示在地图上。
我尝试了一个GML
来自Geoserver
(对源代码进行小的修改)的文件,并且openlayers 3
似乎没有问题消化它。
我错过了什么,还是GML
解析器不支持自定义文件?
代码:
(function() {} (
'use strict';
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.transform([0, 30], 'EPSG:4326', 'EPSG:3857'),
zoom: 2
})
});
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "/echo/xml/", true);
xmlhttp.onload = function () {
var format = new ol.format.GML2();
var xmlDoc = xmlhttp.responseXML;
// Read and parse all features in XML document
var features = format.readFeatures(xmlDoc, {
featureProjection: 'EPSG:4326',
dataProjection: 'EPSG:3857'
});
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);
};
xmlhttp.send();
));
原始GML
文件可在IOC 站 GML获得。我在本地制作了一份副本以避免CORS
.