Openlayers:动态地将矢量添加到现有功能

Openlayers : dynamically add vector to existing feature

我从 gpx 文件创建一个特征:

var gpxFeatures = (new OlFormat.GPX()).readFeatures(GPXSTRING,{
                dataProjection:'EPSG:4326',
                featureProjection:'EPSG:3857'
            });

现在我想为这个特征动态添加一个向量。我知道矢量的两端。

这可能吗?

我找到了解决办法。

O 不要将向量添加到要素中,而是创建一个新要素并将其添加到图层中。

var vectorFeature = new OlFeature({
                        geometry: new OlGeom.LineString([OlProj.fromLonLat(previousLastKnownPosition), OlProj.fromLonLat(newPosition)])
                    });

layer.getSource().addFeatures([vectorFeature, pointFeature]);