首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Openlayers 3-在沿线的某处之间的LineString中插入点

Openlayers 3-在沿线的某处之间的LineString中插入点
EN

Stack Overflow用户
提问于 2017-02-20 15:50:12
回答 1查看 511关注 0票数 1

我正在寻找一种聪明的方式来插入一个点到一个LineString的Openlayers-3。今天,我沿着完整的LineString保存了最近的点,用forEachSegment做了一个循环,找到最近点的线段,并在这个线段的起点和终点之间插入点。最后,我将新的部分返回到完整LineString的几何结构中。

起作用了。但是有没有更聪明、更短的解决方案呢?

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

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

发布于 2017-02-20 16:48:50

看起来每个分段都不能处理包含多个分段的行。为了在一个线串中插入多个点,我现在使用这个解决方案:

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42339085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档