将点添加到现有线串

mar*_*aos 3 openlayers

是否可以在openlayers中为现有的线串添加点?当我们到达客户端时,我有一个来自服务器的点流.目前,我能看到这样做的唯一方法是每次新点进入时从我收到的最后一点到新点画一条线,如下所示:

Drawer.prototype.drawPoint = function(point)
{
    var line = new OpenLayers.Geometry.LineString([this.lastPoint, point]);
    var lineFeature = new OpenLayers.Feature.Vector(line, null, this.style);
    this.lineLayer.addFeatures([lineFeature]);
    this.lastPoint = point;
}
Run Code Online (Sandbox Code Playgroud)

这似乎效率低下.显然,我可以保留所有点的数组,并在每次出现新点时重新绘制整行,但这似乎效率低下.

igo*_*rti 5

OpenLayers.Geometry.LineString有一个addPoint(point, index)通过OpenLayers.Geometry.MultiPoint它派生的类的方法,看一下源代码.

所以你的解决方案应该简单如下:

line.addPoint(point); //second parameter(index) is optional
Run Code Online (Sandbox Code Playgroud)

您可能还需要redraw()在图层上调用方法.

  • 就是那个.我得出结论,OpenLayers网站上的文档毫无价值!(http://dev.openlayers.org/releases/OpenLayers-2.7/doc/apidocs/files/OpenLayers/Geometry/LineString-js.html).需要显示继承的方法. (3认同)