我使用openlayers 5和angular6。我得到了矢量层的一些功能,并使用它们将它们推送到了阵列广告中,我尝试动态创建LineString。
到目前为止,这是我的代码
import * as Extent from 'ol/extent.js';
import Geometry from 'ol/geom/Geometry.js';
import LineString from 'ol/geom/LineString.js';
extent: Extent;
linestring: LineString;
geometry: Geometry;
然后是功能
let features = this.vectorsource.getFeatures();
let pointsline=[];
features.forEach((item) => {
if ( item.values_.clicked ){
var aa = item.getGeometry().getExtent();
var oo = Extent.getCenter(aa);
pointsline.push('['+oo+']');
}
});
if (pointsline.length > 1) {
this.linestring = new LineString({coordinates:pointsline});
this.clicledRouteFeature = new Feature({
Geometry: this.linestring
});
this.clicledRouteFeature.setStyle(new Style({
stroke: new Stroke({
color: 'red',
width: 22
})
}));
this.vectorsource.addFeatures(this.clicledRouteFeature);
}
这是我所能做的。请帮助我完成这项工作。我收到与该行ERROR TypeError: Cannot read property 'length' of undefined
相关的错误this.linestring = new LineString({coordinates:pointsline});
有什么建议吗?
谢谢
答案 0 :(得分:0)
这是非常简单的修复。更改以下行
pointsline.push('['+oo+']');
到
pointsline.push([oo]);