Skip to content

Leaflet: Programmatically add polygon which is then editable using draw controls #4375

Closed Answered by falkoschindler
amks1 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @amks1,

Support for editing Leaflet layers is still pretty limited (see PR #3586). To make layers editable, we need to somehow add them to the FeatureGroup of drawn items. Here is quite a hacky example how this could be done:

m = ui.leaflet(center=(51.505, -0.09), draw_control={}).classes('h-96')
ui.button('Add marker', on_click=lambda: ui.run_javascript(f'''
    const m = getElement('{m.id}');
    m.map.eachLayer(layer => {{
        if (layer instanceof L.FeatureGroup) {{
            layer.addLayer(L.marker([51.505, -0.09]));
        }}
    }});
'''))

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@amks1
Comment options

Answer selected by amks1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants