55
66
77class Draw (JSCSSMixin , MacroElement ):
8- """
8+ '''
99 Vector drawing and editing plugin for Leaflet.
1010
1111 Parameters
@@ -25,22 +25,35 @@ class Draw(JSCSSMixin, MacroElement):
2525 edit_options : dict, optional
2626 The options used to configure the edit toolbar. See
2727 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.
2831
2932 Examples
3033 --------
3134 >>> m = folium.Map()
3235 >>> Draw(
3336 ... export=True,
3437 ... filename="my_data.geojson",
38+ ... show_geometry_on_click=False,
3539 ... position="topleft",
3640 ... draw_options={"polyline": {"allowIntersection": False}},
3741 ... edit_options={"poly": {"allowIntersection": False}},
42+ ... on={
43+ ... "click": JsCode(
44+ ... """
45+ ... function(event) {
46+ ... alert(JSON.stringify(this.toGeoJSON()));
47+ ... }
48+ ... """
49+ ... )
50+ ... },
3851 ... ).add_to(m)
3952
4053 For more info please check
4154 https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html
4255
43- """
56+ '''
4457
4558 _template = Template (
4659 """
@@ -68,11 +81,15 @@ class Draw(JSCSSMixin, MacroElement):
6881 console.log(coords);
6982 });
7083 {%- endif %}
84+
85+ {%- for event, handler in this.on.items() %}
86+ layer.on(
87+ "{{event}}",
88+ {{handler}}
89+ );
90+ {%- endfor %}
7191 drawnItems_{{ this.get_name() }}.addLayer(layer);
7292 });
73- {{ this._parent.get_name() }}.on('draw:created', function(e) {
74- drawnItems_{{ this.get_name() }}.addLayer(e.layer);
75- });
7693 {% if this.export %}
7794 document.getElementById('export').onclick = function(e) {
7895 var data = drawnItems_{{ this.get_name() }}.toGeoJSON();
@@ -111,6 +128,7 @@ def __init__(
111128 show_geometry_on_click = True ,
112129 draw_options = None ,
113130 edit_options = None ,
131+ on = None ,
114132 ):
115133 super ().__init__ ()
116134 self ._name = "DrawControl"
@@ -120,6 +138,7 @@ def __init__(
120138 self .show_geometry_on_click = show_geometry_on_click
121139 self .draw_options = draw_options or {}
122140 self .edit_options = edit_options or {}
141+ self .on = on or {}
123142
124143 def render (self , ** kwargs ):
125144 super ().render (** kwargs )
0 commit comments