本文介绍了Openlayers3:中止绘图交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的html使用绘制交互来手动绘制路线

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

如果用户中止提示或不输入名称,我如何停止交互/删除已创建的线串?我尝试通过定义唯一的tempID并将其从源中删除来进行尝试,但这似乎不起作用.

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

推荐答案

请等到您的功能已添加ol.source.Vector:

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

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

这篇关于Openlayers3:中止绘图交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 13:10