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
@@ -25,22 +25,35 @@ class Draw(JSCSSMixin, MacroElement):
25
25
edit_options : dict, optional
26
26
The options used to configure the edit toolbar. See
27
27
https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#editpolyoptions
28
+ on : dict, optional
29
+ Event handlers to attach to the created layer. Pass a mapping from the
30
+ names of the events to their `JsCode` handlers.
28
31
29
32
Examples
30
33
--------
31
34
>>> m = folium.Map()
32
35
>>> Draw(
33
36
... export=True,
34
37
... filename="my_data.geojson",
38
+ ... show_geometry_on_click=False,
35
39
... position="topleft",
36
40
... draw_options={"polyline": {"allowIntersection": False}},
37
41
... edit_options={"poly": {"allowIntersection": False}},
42
+ ... on={
43
+ ... "click": JsCode(
44
+ ... """
45
+ ... function(event) {
46
+ ... alert(JSON.stringify(this.toGeoJSON()));
47
+ ... }
48
+ ... """
49
+ ... )
50
+ ... },
38
51
... ).add_to(m)
39
52
40
53
For more info please check
41
54
https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html
42
55
43
- """
56
+ '''
44
57
45
58
_template = Template (
46
59
"""
@@ -68,11 +81,15 @@ class Draw(JSCSSMixin, MacroElement):
68
81
console.log(coords);
69
82
});
70
83
{%- endif %}
84
+
85
+ {%- for event, handler in this.on.items() %}
86
+ layer.on(
87
+ "{{event}}",
88
+ {{handler}}
89
+ );
90
+ {%- endfor %}
71
91
drawnItems_{{ this.get_name() }}.addLayer(layer);
72
92
});
73
- {{ this._parent.get_name() }}.on('draw:created', function(e) {
74
- drawnItems_{{ this.get_name() }}.addLayer(e.layer);
75
- });
76
93
{% if this.export %}
77
94
document.getElementById('export').onclick = function(e) {
78
95
var data = drawnItems_{{ this.get_name() }}.toGeoJSON();
@@ -111,6 +128,7 @@ def __init__(
111
128
show_geometry_on_click = True ,
112
129
draw_options = None ,
113
130
edit_options = None ,
131
+ on = None ,
114
132
):
115
133
super ().__init__ ()
116
134
self ._name = "DrawControl"
@@ -120,6 +138,7 @@ def __init__(
120
138
self .show_geometry_on_click = show_geometry_on_click
121
139
self .draw_options = draw_options or {}
122
140
self .edit_options = edit_options or {}
141
+ self .on = on or {}
123
142
124
143
def render (self , ** kwargs ):
125
144
super ().render (** kwargs )
0 commit comments