Openlayers3:中止绘制交互

2024-05-04

我在 html 中使用绘制交互来手动绘制路线

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

并在此函数中询问名称

// 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 并将其从源中删除,但这似乎不起作用。


等到你的功能被added http://openlayers.org/en/v3.9.0/apidoc/ol.source.VectorEvent.html#event:addfeature to ol.source.Vector:

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

    console.log("Map Interaction(Route): deactivated");
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Openlayers3:中止绘制交互 的相关文章