对于任何有 leaflet 或 leaflet.draw 插件经验的人:
我想在不使用 leaflet.draw
中的工具栏的情况下开始绘制多边形。通过在线搜索(它不在主文档中),我设法找到了允许不使用工具栏 (layer.editing.enable();
) 进行编辑的属性。我似乎无法找到如何在没有工具栏按钮的情况下开始绘制多边形。任何帮助将不胜感激!
谢谢你:)
最佳答案
这个简单的代码对我有用:
new L.Draw.Polyline(map, drawControl.options.polyline).enable();
只需将它放入自定义按钮的 onclick 处理程序中(或任何您想要的地方)。
变量 map
和 drawControl
是对传单 map 和绘图控件的引用。
深入研究源代码 (leaflet.draw-src.js),您可以找到绘制其他元素以及编辑或删除它们的函数。
new L.Draw.Polygon(map, drawControl.options.polygon).enable()
new L.Draw.Rectangle(map, drawControl.options.rectangle).enable()
new L.Draw.Circle(map, drawControl.options.circle).enable()
new L.Draw.Marker(map, drawControl.options.marker).enable()
new L.EditToolbar.Edit(map, {
featureGroup: drawControl.options.featureGroup,
selectedPathOptions: drawControl.options.edit.selectedPathOptions
})
new L.EditToolbar.Delete(map, {
featureGroup: drawControl.options.featureGroup
})
我希望这对你也有用。
编辑:
L.EditToolbar.Edit
和 L.EditToolbar.Delete
类公开了以下有用的方法:
- enable(): 启动编辑/删除模式
- disable(): 返回标准模式
- save():保存更改(它触发 draw:edited/draw:deleted 事件)
- revertLayers():撤消更改
关于javascript - Leaflet.draw映射: How to initiate the draw function without toolbar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15775103/