传单.绘制映射:如何在没有工具栏的情况下启动绘制功能
Leaflet.draw mapping: How to initiate the draw function without toolbar?
对于有传单或传单经验的人。画插件:
我想在不使用leaflet.draw
工具栏的情况下开始绘制多边形。我通过在线搜索(不在主要文档中)找到了允许编辑而不使用工具栏(layer.editing.enable();
)的属性。我似乎找不到如何开始绘制没有工具栏按钮的多边形。任何帮助将非常感激!
谢谢你
这个简单的代码为我工作:
new L.Draw.Polyline(map, drawControl.options.polyline).enable();
只需将其放入自定义按钮的onclick处理程序中(或任何您想要的地方)。
变量map
和drawControl
是对你们的传单图和图纸控制的引用。
深入源代码(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():保存更改(触发绘制:编辑/绘制:删除事件)
- revertLayers():撤销更改
我认为值得一提的是Jacob Toyes对这个问题的回答。你总是在传单上画处理者。绘制 -不能直接使用图层。如果你想编辑一个图层,你可以使用像这样保存在图层editing
字段中的处理程序:layer.editing.enable();
。如果你想创建一个新图层,你首先创建一个新的handler:
// Define you draw handler somewhere where you click handler can access it. N.B. pass any draw options into the handler
var polygonDrawer = new L.Draw.Polyline(map);
// Assumming you have a Leaflet map accessible
map.on('draw:created', function (e) {
var type = e.layerType,
layer = e.layer;
// Do whatever you want with the layer.
// e.type will be the type of layer that has been draw (polyline, marker, polygon, rectangle, circle)
// E.g. add it to the map
layer.addTo(map);
});
// Click handler for you button to start drawing polygons
$('#draw_poly').click(function() {
polygonDrawer.enable();
});
现在在传单上实际上有一个例子。绘制 github页面:https://github.com/Leaflet/Leaflet.draw/blob/develop/docs/examples/edithandlers.html
然而,我认为处理程序还没有很好地记录在那里。
就像上面所说的,L.EditToolbar.Edit
和L.EditToolbar.Delete
公开了一些有趣的方法和事件,比如editstart和editstop。没有提到的是,这两个类本身是从L.Handler
派生的。
虽然BaCH的解决方案可能是最好的,但我想添加一个单行解决方案,它实际上更面向未来(而不是挖掘未记录的传单绘制方法)和最简单的。
document.querySelector('.leaflet-draw-draw-polygon').click();
就是这样。你只是利用了工具栏的存在,但实际上,你没有使用它。您可以以任何方式以编程方式初始化绘图。你也可以使用CSS隐藏工具栏。
如果你想开始绘制一个不同的形状,使用以下类之一:
.leaflet-draw-draw-polyline
.leaflet-draw-draw-rectangle
.leaflet-draw-draw-circle
.leaflet-draw-draw-marker
.leaflet-draw-draw-circlemarker
对于圆我已经算出来了,但是对于多边形也是一样的。其实很简单。希望下面的代码回答你的问题,但如果不是让我知道,我可以张贴更多的要点或其他东西。
// Creates the circle on the map for the given latLng and Radius
// If the createdWithAddress flag is true, the circle will not update
// it's address according to its position.
createCircle: function(latLng, radius, createdWithAddress) {
if (!this.circle) {
var self = this,
centerIcon,
centerMarker;
centerIcon = new L.Icon({
iconUrl: '/assets/location_pin_24px.png',
iconSize: [24, 24],
iconAnchor: [12, 24],
shadowUrl: '/assets/marker-shadow.png',
shadowSize: [20, 20],
shadowAnchor:[6, 20]
})
// Setup the options for the circle -> Override icons, immediately editable
options = {
stroke: true,
color: '#333333',
opacity: 1.0,
weight: 4,
fillColor: '#FFFFFF',
moveIcon: centerIcon,
resizeIcon: new L.Icon({
iconUrl: '/assets/radius_handle_18px.png',
iconSize: [12, 12],
iconAnchor: [0,0]
})
}
if (someConfigVarYouDontNeedToKnow) {
options.editable = false
centerMarker = new L.Marker(latLng, { icon:centerIcon })
} else {
options.editable = true
}
// Create our location circle
// NOTE: I believe I had to modify Leaflet or Leaflet.draw to allow for passing in
// options, but you can make it editable with circle.editing.enable()
this.circle = L.circle([latLng.lat, latLng.lng], radius, options)
// Add event handlers to update the location
this.circle.on('add', function() {
if (!createdWithAddress) {
self.reverseGeocode(this.getLatLng())
}
self.updateCircleLocation(this.getLatLng(), this.getRadius())
self.updateMapView()
})
this.circle.on('edit', function() {
if (self.convertLatLngToString(this.getLatLng()) !== self.getLocationLatLng()) {
self.reverseGeocode(this.getLatLng())
}
self.updateCircleLocation(this.getLatLng(), this.getRadius())
self.updateMapView()
})
this.map.addLayer(this.circle)
if (centerMarker) {
centerMarker.addTo(this.map)
this.circle.redraw()
centerMarker.update()
}
}
},
对不起,很多只是噪音,但它应该给你一个如何去做的想法。您可以像前面所说的那样使用edit .enable()/.disable()来控制编辑。
如果有问题一定要留言。祝你好运。
- 删除CKEditor工具栏按钮,但不删除功能
- Sencha触摸:工具栏无法正常滚动
- 使用网格ID隐藏剑道网格工具栏
- 如何在IE中隐藏菜单栏,工具栏
- 将此事件添加到工具栏
- ExtJS--如何居中对齐两个工具栏按钮
- 在某些URL上显示火狐插件工具栏
- 如何将自定义图标添加到 JqGrid 顶级工具栏按钮
- 文本编辑工具栏解决方案 (js),具有低级自定义
- Ext JS工具栏和痕迹导航示例在JSFiddle中不起作用
- 如何在较新版本的 Free-JQGard 中重置搜索工具栏和搜索过滤器
- 尝试绘制从鼠标位置工具栏中选择的形状 - HTML5 CANVAS
- ExtJS 4-在调整窗口大小时,溢出的工具栏会失去控制器
- 如何在ExtJS 4.2中聚焦工具栏的按钮
- 单击Firefox工具栏按钮中的坐标
- 为什么我的 init() 函数没有运行?(安装 Firefox 扩展时自动添加工具栏按钮,但仅在首次运行时)
- 让jQuery顶部工具栏不那么跳跃
- JQGridPageing在通过过滤器工具栏应用过滤器后非常慢
- 如何在mozilla firefox扩展中更改自定义工具栏按钮的颜色
- 传单.绘制映射:如何在没有工具栏的情况下启动绘制功能