我认为标题很清楚,我正在使用 openLayer 库(v4.6.5),并且我试图在加载页面时在地图中加载本地 GPX 文件。在官方文档中,在 GPX 数据示例中(https://openlayers.org/en/latest/examples/gpx.html?q=data https://openlayers.org/en/latest/examples/gpx.html?q=data)这对于 url 来说似乎非常简单。
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'https://openlayers.org/en/v4.6.5/examples/data/gpx/fells_loop.gpx',
format: new ol.format.GPX()
}),
style: function(feature) {
return style[feature.getGeometry().getType()];
}
});
我的问题是,我需要在 ol.source.Vector 对象中设置什么选项来加载本地 GPX 文件,而不是设置 url 选项?提前致谢。
解决方案1
只需使用 OpenLayers 库中提供的拖放组件即可。演示地址:https://openlayers.org/en/latest/examples/drag-and-drop.html https://openlayers.org/en/latest/examples/drag-and-drop.html(拖放GPX进行测试)
解决方案2
如果您想要/需要手动执行操作,则需要删除 URL 引用和格式。
然后,当您执行拖放(或其他操作)时,您会获得 GPS 文件内容,然后使用以下命令解析它:
var GPXfeatures = (new ol.format.GPX()).readFeatures('yourGPXstring', {featureProjection: 'EPSG:3857'})
然后将功能添加到源中
vector.getSource().addFeatures(GPXfeatures)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)