5
5
6
6
7
7
class Draw (JSCSSMixin , MacroElement ):
8
- """
8
+ '''
9
9
Vector drawing and editing plugin for Leaflet.
10
10
11
11
Parameters
@@ -28,22 +28,35 @@ class Draw(JSCSSMixin, MacroElement):
28
28
edit_options : dict, optional
29
29
The options used to configure the edit toolbar. See
30
30
https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#editpolyoptions
31
+ on : dict, optional
32
+ Event handlers to attach to the created layer. Pass a mapping from the
33
+ names of the events to their `JsCode` handlers.
31
34
32
35
Examples
33
36
--------
34
37
>>> m = folium.Map()
35
38
>>> Draw(
36
39
... export=True,
37
40
... filename="my_data.geojson",
41
+ ... show_geometry_on_click=False,
38
42
... position="topleft",
39
43
... draw_options={"polyline": {"allowIntersection": False}},
40
44
... edit_options={"poly": {"allowIntersection": False}},
45
+ ... on={
46
+ ... "click": JsCode(
47
+ ... """
48
+ ... function(event) {
49
+ ... alert(JSON.stringify(this.toGeoJSON()));
50
+ ... }
51
+ ... """
52
+ ... )
53
+ ... },
41
54
... ).add_to(m)
42
55
43
56
For more info please check
44
57
https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html
45
58
46
- """
59
+ '''
47
60
48
61
_template = Template (
49
62
"""
@@ -78,11 +91,19 @@ class Draw(JSCSSMixin, MacroElement):
78
91
console.log(coords);
79
92
});
80
93
{%- endif %}
94
+
95
+ {%- for event, handler in this.on.items() %}
96
+ layer.on(
97
+ "{{event}}",
98
+ {{handler}}
99
+ );
100
+ {%- endfor %}
81
101
drawnItems_{{ this.get_name() }}.addLayer(layer);
82
102
});
83
103
{{ this._parent.get_name() }}.on('draw:created', function(e) {
84
104
drawnItems_{{ this.get_name() }}.addLayer(e.layer);
85
105
});
106
+
86
107
{% if this.export %}
87
108
document.getElementById('export').onclick = function(e) {
88
109
var data = drawnItems_{{ this.get_name() }}.toGeoJSON();
@@ -122,6 +143,7 @@ def __init__(
122
143
show_geometry_on_click = True ,
123
144
draw_options = None ,
124
145
edit_options = None ,
146
+ on = None ,
125
147
):
126
148
super ().__init__ ()
127
149
self ._name = "DrawControl"
@@ -132,6 +154,7 @@ def __init__(
132
154
self .show_geometry_on_click = show_geometry_on_click
133
155
self .draw_options = draw_options or {}
134
156
self .edit_options = edit_options or {}
157
+ self .on = on or {}
135
158
136
159
def render (self , ** kwargs ):
137
160
super ().render (** kwargs )
0 commit comments