如何获取GPX文件的范围以将其显示在地图上(使用openalyers) [英] How to get the extent of a GPX file to display it on a map (using openalyers)

查看:279
本文介绍了如何获取GPX文件的范围以将其显示在地图上(使用openalyers)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究openlayers 5.x的GPX源示例,可从 https:获得: //openlayers.org/en/latest/examples/gpx.html

I am working on the GPX source example of openlayers 5.x available at https://openlayers.org/en/latest/examples/gpx.html

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

I am able to successfully load my GPX file and to display it on a map, but I have been unable to get its extent to know the min/max latitudes and longitudes to fit it dynamically on the map.

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

This is my code (identical to the example):

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文件可以正确显示,但是我无法获得调整地图大小以进行缩放的程度.我已经尝试过:

The GPX file displays correctly but I am unable to get its extent to resize the map to zoom on it. I have tried:

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

没有任何可解析的功能:

which gives no features to parse:

length: 0
__proto__: Array(0)

请注意,来源已在此处,并在地图上正确显示:

note that the source is there, and displays correctly on the map:

console.log(GpxVector.getSource());

给予:

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

但没有范围:

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

给予:

[Infinity, Infinity, -Infinity, -Infinity]

VectorLayer也没有范围:

Also the VectorLayer has no extent:

console.log(GpxVector.getExtent());

给予:

undefined

有人可以告诉我如何访问GPX文件的范围或至少它的要点,以便我自己计算吗?

Can someone help me and tell me how I can access the extents of the GPX file or at least its points so I can calculate it myself ?

任何帮助将不胜感激!

推荐答案

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

The source won't load unless the layer is visible on the map. If you set an arbitrary opening center and zoom in the view, the source will load and then you can fit:

  // 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);

这篇关于如何获取GPX文件的范围以将其显示在地图上(使用openalyers)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆