Openlayers 3 - 将一个点插入沿线的某个位置之间的LineString

时间:2017-02-20 07:50:12

标签: javascript openlayers-3

我正在寻找一种使用Openlayers-3将点插入LineString的智能方法。今天,我沿着完整的LineString保存了最近的点,用forEachSegment创建了一个循环,找到最近点的段,并在该段的开始和结束之间插入点。最后,我将新部分重新放回完整LineString的几何体。

它正在运作。但任何更智能,更短的解决方案?

谢谢&干杯! 安德烈亚斯。

pp=modifyfeatures.item(g).getGeometry().forEachSegment(function (start, end){
 waylinexy=new Array();
 waylinexy.push(start);
 waylinexy.push(end);
 var segment = new ol.Feature({geometry:new ol.geom.LineString(waylinexy, 'XY')});
 pp.push(start); 
 if (segment.getGeometry().getClosestPoint(cmpos).toString()==cmpos.toString()){pp.push(cmpos); }   
 pp.push(end); 
 return pp; 
});
var ps = new ol.Feature({geometry:new ol.geom.LineString(pp, 'XY')});
modifyfeatures.item(g).getGeometry().setCoordinates(pp);

1 个答案:

答案 0 :(得分:1)

看起来foreachsegment不适用于包含多个细分的行。为了将多个点插入到线串中,我现在使用此解决方案:

p=new Array();
cmpos=modifyfeatures.item(g).getGeometry().getClosestPoint(eventcoord);
linegeo=modifyfeatures.item(g).getGeometry().getCoordinates();
for (a=0;a<linegeo.length-1;a++)
{
 start=linegeo[a];
 end=linegeo[a+1];
 var segment = new ol.Feature({geometry:new ol.geom.LineString([start, end], 'XY')});
 p.push(start); 
 if (segment.getGeometry().getClosestPoint(cmpos).toString()==cmpos.toString()){p.push(cmpos); }   
 p.push(end); 
}
modifyfeatures.item(g).getGeometry().setCoordinates(p);