首页 > 解决方案 > Leaflet.draw 发出事件以停止绘图

问题描述

使用传单绘制,当我用多边形绘制时,我需要在矩形的第二个点固定时结束绘制。监听 'drawvertex' 事件两次,在

mymap.on(L.Draw.Event.DRAWVERTEX, function(e){
    console.log("draw 1st vertex--")
    // console.log(e)

    mymap.on(L.Draw.Event.DRAWVERTEX, function(e){

        console.log("draw 2nd vertex, now need to be closed! ---", e)

        let tmp = e.layers;

        // mymap.emit(L.Draw.Event.DRAWSTOP, function (e){
        //  console.log("draw stopped...")
        // })

        // mymap.emit('draw:drawstop', function (e){
        //  console.log("draw stopped...")
        // })

        // tmp.completeShape()

        // drawnItems.completeShape()
    })
    // if(e.layerType == "polyne"){
    //  console.log(" e la retta..")
    // }
})

我尝试使用 emit 和 completeShape 函数,但它不起作用,我尝试使用按钮“完成”的相同方法(当您单击多边形进行绘制时)但我没有在源代码上找到该方法。这是我的密码箱https://codesandbox.io/s/romantic-jepsen-7esnz?file=/index.html

标签: javascriptleafletleaflet-draw

解决方案


您可以自动触发第二次单击以完成形状

mymap.on(L.Draw.Event.DRAWVERTEX, function (e) {

    const layerIds = Object.keys(e.layers._layers);

    if (layerIds.length > 1) {

        const secondVertex = e.layers._layers[layerIds[1]]._icon;

        requestAnimationFrame(() => secondVertex.click());

    }
});

推荐阅读