javascript - 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
解决方案
您可以自动触发第二次单击以完成形状
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());
}
});
推荐阅读
- vba - SignNow API 与 Ms Access VBA 的集成
- c# - 如何将 MessageEnvelope xmlns 转换为 json
- android - 适用于 Unity 的 Google In-App Review 未显示,未引发错误
- python - 我的代码没有读取收件箱中的所有电子邮件
- c++ - 新手:VisualStudioCode:如何从多个 cpp 文件中编译一个 exec?
- angular - 如何解决 Angular 10 primeng NullInjectorError:没有 ConfirmationService 的提供者?
- wordpress - 使用 wp_handle_upload() 和 add_post_meta() 将图像放入 WordPress 自定义帖子类型(非 ACF)的自定义元字段
- qliksense - 如果总和在两个值之间,则计算不同的 ID [Qlik Sense]
- asp.net-mvc - 如何使用entities.Database.SqlQuery 获取字段的所有值?
- r - 聚合函数不想在季度中尊重月份