Strycer
Strycer

Reputation: 171

Openlayers3: Abort the Draw Interaction

i am using for my html a draw interaction for drawing routes manually

// manual route creation event
            $('#createRoute').click(function() {
                // remove previous interactions
                map.removeInteraction(draw);

                // create linestring interaction
                draw = new ol.interaction.Draw({
                    source: routeSource,
                    type: ('LineString'),

                })

                // add interaction to map
                map.addInteraction(draw);

                draw.on('drawstart', function(event) {
                    console.log("Map Interaction(Route): activated");
                });

                draw.on('drawend', function(event) {
                    saveRoute(event);
                    map.removeInteraction(draw);

                    console.log("Map Interaction(Route): deactivated");
                  });
            });

and asking for a name in this function

// saving a route as defined route-object
            function saveRoute(event) {
                // saving ol.objects
                var feature = event.feature;
                var lineString = feature.getGeometry();
                var newWaypoints = lineString.getCoordinates();

                // setting tempid in case of abortion
                feature.setId("tempID");

                // prompt popup for routeName
                var routeName = prompt("Name der Route eingeben", "");
                var newName;

                // save route object
                if (routeName != null && routeName.length > 0) {
                    newName = routeName;

                    console.log(routeName);
                    console.log(newWaypoints);
                }else
                {
                    console.log("Route creation aborted");
                }
            }

if the user aborts the prompt or enters no name, how can i stop the interaction/delete the linestring which has been created? I tried it by defining a unique tempID and delete it from the source but this doesnt seem to work..

Upvotes: 3

Views: 1940

Answers (1)

Jonatas Walker
Jonatas Walker

Reputation: 14150

Wait until your feature is added to ol.source.Vector:

your_source.on('addfeature', function(event) {
    saveRoute(event);
    map.removeInteraction(draw);

    console.log("Map Interaction(Route): deactivated");
});

Upvotes: 3

Related Questions