简体   繁体   English

如何在openLayer地图中加载本地gpx文件?

[英]How load local gpx file in a openLayer map?

I think than the title is pretty clear, I am using openLayer library(v4.6.5) and I am trying to load a local GPX file in my map when the page is loaded. 我认为标题不是很清楚,我正在使用openLayer库(v4.6.5),并且试图在加载页面时在地图中加载本地GPX文件。 In the official documentation, in GPX Data example ( https://openlayers.org/en/latest/examples/gpx.html?q=data ) that seems to be pretty straightforward to do with an url. 在官方文档的GPX数据示例( 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()];
    }
  });

My question is, instead of setting url option, what option I need to set in ol.source.Vector object to load a local GPX file? 我的问题是,需要设置ol.source.Vector对象中的哪个选项来加载本地GPX文件,而不是设置url选项? Thanks in advance. 提前致谢。

Solution 1 解决方案1

Just use the drag & drop component provided within OpenLayers library. 只需使用OpenLayers库中提供的拖放组件。 Demo at https://openlayers.org/en/latest/examples/drag-and-drop.html (drag and drop a GPX to test) https://openlayers.org/en/latest/examples/drag-and-drop.html上进行演示(拖放GPX进行测试)

Solution 2 解决方案2

If you want/need to do things manually, you need to remove the URL reference and the format. 如果您希望/需要手动处理,则需要删除URL引用和格式。

Then, you get GPS file content when you do a drag and drop (or another action) then parse it with 然后,当您进行拖放(或其他操作)然后解析为

var GPXfeatures = (new ol.format.GPX()).readFeatures('yourGPXstring', {featureProjection: 'EPSG:3857'})

Then you add the features to the source with 然后使用

vector.getSource().addFeatures(GPXfeatures)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM