Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions folium/plugins/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand Down
27 changes: 27 additions & 0 deletions tests/plugins/test_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading