Openlayers:从请求加载坐标数据

Openlayers: Load coordinate-data from request

我想从 http 请求加载坐标数据并将其绘制为线串。

到目前为止我有:

var coordinates = [[30.433333,19.066667], [30.732778,19.704444],[30.832778,19.84444]]; 
var featurestreet=new ol.Feature({
   geometry: new ol.geom.LineString(coordinates),
   name: 'xyz'
});

但现在我想从这样的文件中加载矢量数据:

var stree = new ol.layer.Vector({
    source: new ol.source.Vector(
{
    url: 'points.txt',
        format: new ol.format.GPX(), //what format to use here?
        name:'stree'
    })

  }); 
map.addLayer(stree);

正确的格式是什么?我想让它非常简单和压缩,所以我不想在这个数据中使用任何放大的 xml-代码。

最好将坐标包装为 GeoJSON:

{ "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "LineString", "coordinates":
[[30.433333,19.066667], [30.732778,19.704444],[30.832778,19.84444]]
} } ] }

如果您的 .txt 文件只包含坐标数组,您可以像 http://openlayers.org/en/v4.6.5/apidoc/ol.source.Vector.html 中一样使用自定义加载程序,并将 GeoJSON 页眉和页脚字符串添加到 xhr.responseText in vectorSource.getFormat() .readFeatures()

要在 GeoJSON 中包含名称:

{ "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "name": "xyz" }, "geometry": { "type": "LineString", "coordinates":
[[30.433333,19.066667], [30.732778,19.704444],[30.832778,19.84444]]
} } ] }