diff --git a/folium/plugins/draw.py b/folium/plugins/draw.py index 62bc95a589..4e7b3afa06 100644 --- a/folium/plugins/draw.py +++ b/folium/plugins/draw.py @@ -126,8 +126,10 @@ class Draw(JSCSSMixin, MacroElement): var coords = JSON.stringify(layer.toGeoJSON()); {%- if this.show_geometry_on_click %} layer.on('click', function() { - alert(coords); - console.log(coords); + // Read the geometry now, so edits made after drawing show up. + var geometry = JSON.stringify(layer.toGeoJSON()); + alert(geometry); + console.log(geometry); }); {%- endif %} diff --git a/tests/plugins/test_draw.py b/tests/plugins/test_draw.py index 507474de2a..3c862c025c 100644 --- a/tests/plugins/test_draw.py +++ b/tests/plugins/test_draw.py @@ -109,3 +109,30 @@ def test_draw_export_position_corners(): def test_draw_export_position_invalid(): with pytest.raises(ValueError, match="export_position must be one of"): plugins.Draw(export=True, export_position="middle") + + +def test_draw_click_shows_current_geometry(): + """The click alert reads the geometry on click, not when it was drawn. + + Otherwise a shape that was edited afterwards still shows its original + coordinates. + """ + m = folium.Map([45.0, 3.0], zoom_start=4) + m.add_child(plugins.Draw()) + + out = m._parent.render() + + start = out.index("layer.on('click', function() {") + handler = out[start : out.index("});", start)] + assert "layer.toGeoJSON()" in handler + assert "alert(geometry);" in handler + assert "alert(coords)" not in out + + +def test_draw_no_click_alert(): + m = folium.Map([45.0, 3.0], zoom_start=4) + m.add_child(plugins.Draw(show_geometry_on_click=False)) + + out = normalize(m._parent.render()) + + assert "alert(" not in out