Openlayers 3 读取扩展 GPX

Openlayers 3 readExtensions GPX

我是 javascript 的新手,但我正在尝试使用 openlayers 3 从 GPX 轨道读取心率扩展。

Sample GPX track point

拖放交互接受 GPX 格式的构造函数。我可以通过传递 ol.format.GPX 构造函数来读取基本信息(lat、lon、ele、time),但我无法弄清楚如何使用 'readExtensions' 选项传递构造函数。

根据 openlayers 文档 (http://openlayers.org/en/v3.1.1/apidoc/ol.format.GPX.html),它应该是一个回调函数,但是当我 运行 我的代码时,我得到一个错误:TypeError: d[g] is not a constructor .

    var dragAndDropInteraction = new ol.interaction.DragAndDrop({
  formatConstructors: [
    //ol.format.GPX(extFeature),
    new ol.format.GPX({
        readExtensions: function(x) {
          return x;
        }
        }),
    ol.format.GeoJSON,
    ol.format.IGC,
    ol.format.KML,
    ol.format.TopoJSON
  ]
});

如何格式化构造函数,以便恢复扩展和标准功能?

您可以创建一个继承自 ol.format.GPX 的自定义格式,并将您的构造函数传递给拖放交互:

var CustomFormat = function() {
  ol.format.GPX.call(this, {
    // custom options
  });
};
ol.inherits(CustomFormat, ol.format.GPX);

var dragAndDropInteraction = new ol.interaction.DragAndDrop({
  formatConstructors: [
    CustomFormat,
    ...
  ]
});

http://jsfiddle.net/6zmprrj7/