diff --git a/folium/plugins/draw.py b/folium/plugins/draw.py index 773bba21e..4a893b17e 100644 --- a/folium/plugins/draw.py +++ b/folium/plugins/draw.py @@ -5,7 +5,7 @@ class Draw(JSCSSMixin, MacroElement): - """ + ''' Vector drawing and editing plugin for Leaflet. Parameters @@ -28,6 +28,9 @@ class Draw(JSCSSMixin, MacroElement): edit_options : dict, optional The options used to configure the edit toolbar. See https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#editpolyoptions + on : dict, optional + Event handlers to attach to the created layer. Pass a mapping from the + names of the events to their `JsCode` handlers. Examples -------- @@ -35,15 +38,25 @@ class Draw(JSCSSMixin, MacroElement): >>> Draw( ... export=True, ... filename="my_data.geojson", + ... show_geometry_on_click=False, ... position="topleft", ... draw_options={"polyline": {"allowIntersection": False}}, ... edit_options={"poly": {"allowIntersection": False}}, + ... on={ + ... "click": JsCode( + ... """ + ... function(event) { + ... alert(JSON.stringify(this.toGeoJSON())); + ... } + ... """ + ... ) + ... }, ... ).add_to(m) For more info please check https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html - """ + ''' _template = Template( """ @@ -78,11 +91,19 @@ class Draw(JSCSSMixin, MacroElement): console.log(coords); }); {%- endif %} + + {%- for event, handler in this.on.items() %} + layer.on( + "{{event}}", + {{handler}} + ); + {%- endfor %} drawnItems_{{ this.get_name() }}.addLayer(layer); }); {{ this._parent.get_name() }}.on('draw:created', function(e) { drawnItems_{{ this.get_name() }}.addLayer(e.layer); }); + {% if this.export %} document.getElementById('export').onclick = function(e) { var data = drawnItems_{{ this.get_name() }}.toGeoJSON(); @@ -122,6 +143,7 @@ def __init__( show_geometry_on_click=True, draw_options=None, edit_options=None, + on=None, ): super().__init__() self._name = "DrawControl" @@ -132,6 +154,7 @@ def __init__( self.show_geometry_on_click = show_geometry_on_click self.draw_options = draw_options or {} self.edit_options = edit_options or {} + self.on = on or {} def render(self, **kwargs): super().render(**kwargs)