0

Following the example Draw Feature

in openlayers 4 i was able to addInteraction and successfully drew a line.

My questions here are

  1. How do i capture all the lines drawn and populate them on the map again?
  2. How do i remove the latest drawn line. (Something like an undo button that keeps removing the latest line)?
1
  1. You can define a drawend function and grab your drawn geometry.

                currentProcess = new ol.interaction.Draw($.extend({
                    source: self.VectorSource,
                    type: 'LineString'
                }, options));
                self.Map.olMap.addInteraction(currentProcess);
                currentProcess.on('drawend', function (e) {
                    console.log("drawend");
                    var format = new ol.format.WKT();
                                                e.feature.getGeometry().transform('EPSG:3857', 'EPSG:32636');
                                                var wkt = format.writeFeature(e.feature);
                                                data.Geom = wkt;
                    self.Map.olMap.removeInteraction(currentProcess);
                });
    

Here is the WKT your geometry data and then with this code you can add feature to layer.

vectorSource.addFeature(geometryData);
  1. You can easily remove your added geometry data from map with removeFeature function.

    featureOverlay.getSource().removeFeature(highlight);

Sources:

addFeature

removeFeature

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.