【问题标题】:How to get the extent of a GPX file to display it on a map (using openalyers)如何获取 GPX 文件的范围以将其显示在地图上(使用 openalyers)
【发布时间】:2019-05-06 23:23:40
【问题描述】:

我正在研究 openlayers 5.x 的 GPX 源代码示例,地址为 https://openlayers.org/en/latest/examples/gpx.html

我能够成功加载我的 GPX 文件并将其显示在地图上,但我无法获得它的范围以了解最小/最大纬度和经度以使其在地图上动态适应。

这是我的代码(与示例相同):

var GpxVector = new VectorLayer({
   source: new VectorSource({
      url: 'https://host.domain.com/filename.gpx',
      format: new GPX(),
   }),
   style: function(feature) {
      return GpxStyle[feature.getGeometry().getType()];
   }
});

map.addLayer(GpxVector);

GPX 文件显示正确,但我无法调整地图大小以对其进行缩放。我试过了:

console.log(GpxVector.getSource().getFeatures());

没有提供解析的特征:

length: 0
__proto__: Array(0)

请注意,来源在那里,并在地图上正确显示:

console.log(GpxVector.getSource());

给予:

c {disposed_: false, pendingRemovals_: {…}, dispatching_: {…}, listeners_: {…}, revision_: 0, …}

但没有范围:

console.log(GpxVector.getSource().getExtent());

给予:

[Infinity, Infinity, -Infinity, -Infinity]

VectorLayer 也没有范围:

console.log(GpxVector.getExtent());

给予:

undefined

谁能帮助我并告诉我如何访问 GPX 文件的范围或至少它的点,以便我自己计算它?

任何帮助将不胜感激!

【问题讨论】:

    标签: openlayers openlayers-5


    【解决方案1】:

    除非图层在地图上可见,否则不会加载源。如果您设置任意打开中心并放大视图,源将加载,然后您可以适应:

      // fit when the source loads
      GpxVector.getSource().on('addfeature', function(){
          map.getView().fit(GpxVector.getSource().getExtent());
      });
    
      // open the view to force a load
      map.getView().setCenter([0, 0]);
      map.getView().setZoom(0);
    

    【讨论】:

    • 太棒了!!!!它运行良好,非常感谢,我被困了至少 5 个小时!
    猜你喜欢
    • 2012-02-09
    • 1970-01-01
    • 2011-03-18
    • 2014-03-10
    • 1970-01-01
    • 2016-08-03
    • 2014-03-29
    • 2013-03-31
    • 1970-01-01
    相关资源
    最近更新 更多