Openlayers:从请求中加载坐标数据

时间:2018-08-31 10:59:47

标签: openlayers

我想从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代码。

1 个答案:

答案 0 :(得分:0)

最好将坐标包装为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标头和页脚字符串添加到vectorSource.getFormat()。readFeatures()中的xhr.responseText中。 >

要将名称包括在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]]
} } ] }