Openlayers3: Abort the Draw Interaction

天大地大妈咪最大 提交于 2019-12-11 03:49:45

问题


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..


回答1:


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");
});


来源:https://stackoverflow.com/questions/32798891/openlayers3-abort-the-draw-interaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!