openlayersopenlayers-5

Openlayers : dynamically add vector to existing feature


I create a feature from a gpx file :

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

Now I wish to dynamically add a vector to this feature. I know both extremity of the vector.

Is this possible ?


Solution

  • I have found a solution.

    O do not add the vector to the feature but I create a new feature and add it to the layer.

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