简单示例';Openlayers GML';?

简单示例';Openlayers GML';?,openlayers,gml,Openlayers,Gml,我急切地想在互联网上找到一个简单的例子,如何将“GML数据”添加到Openlayers v4中(向量)。这似乎是一个简单的问题,但我找不到任何工作示例(我找到了很多Openlayers v2和v3示例) 到目前为止,我有以下代码: [示例.html] <!doctype html> <html lang="en"> <head> <link rel="stylesheet" href="https://openlayers.org/en/v

我急切地想在互联网上找到一个简单的例子,如何将“GML数据”添加到Openlayers v4中(向量)。这似乎是一个简单的问题,但我找不到任何工作示例(我找到了很多Openlayers v2和v3示例)

到目前为止,我有以下代码:

[示例.html]

 <!doctype html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.0.1/css/ol.css" type="text/css">
    <script src="https://openlayers.org/en/v4.0.1/build/ol.js"></script>
  </head>
  <body>
    <h1>My Map</h1>
    <div id="map"></div>
    <script type="text/javascript">
      var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            title: 'Global Imagery',
            source: new ol.source.TileWMS({
              url: 'https://ahocevar.com/geoserver/wms',
              params: {LAYERS: 'nasa:bluemarble', TILED: true}
            })
          }),
          new ol.layer.Vector({
            source: new ol.source.Vector({
                url: 'test.gml',
                format: new ol.format.GML()
            }),
          }),
        ],
        view: new ol.View({
          projection: 'EPSG:4326',
          center: [0, 0],
          zoom: 0,
          maxResolution: 0.703125
        })
      });
    </script>
  </body>
</html>

我的地图
var map=新ol.map({
目标:“地图”,
图层:[
新ol.layer.Tile({
标题:“全球图像”,
来源:新ol.source.TileWMS({
网址:'https://ahocevar.com/geoserver/wms',
参数:{LAYERS:'nasa:bluemarble',TILED:true}
})
}),
新ol.layer.Vector({
来源:新ol.source.Vector({
url:'test.gml',
格式:new ol.format.GML()
}),
}),
],
视图:新ol.view({
投影:‘EPSG:4326’,
中间:[0,0],
缩放:0,
最大分辨率:0.703125
})
});
[test.gml]

<?xml version="1.0" encoding="utf-16"?>
<gml:featureMember
    xsi:schemaLocation="http://www.opengis.net/gml http://schemas.opengis.net/gml/3.1.1/profiles/gmlsfProfile/1.0.0/gmlsf.xsd"
    xmlns:gml="http://www.opengis.net/gml"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <feature:feature fid="OpenLayers.Feature.Vector_107" xmlns:feature="http://example.com/feature">
        <feature:geometry>
            <gml:Point>
                <gml:pos>51.509865, -0.118092</gml:pos>
            </gml:Point>
        </feature:geometry>
    </feature:feature>
</gml:featureMember>

51.509865, -0.118092

请注意,您正在尝试加载一个文件(gml),而不将其托管在Web服务器中。如果打开调试器模式,将出现以下错误

无法加载XMLHttpRequestfile:///C:/test.gml. 跨来源请求 仅支持协议方案:http、数据、chrome、, chrome扩展、https、chrome扩展资源

错误本身给了你提示。 请参阅StackOverflow中发布的问题的答案