jupyter-widgets / ipyleaflet

A Jupyter - Leaflet.js bridge
https://ipyleaflet.readthedocs.io
MIT License
1.47k stars 359 forks source link

Add on_zoom #1214

Closed lopezvoliver closed 6 days ago

lopezvoliver commented 1 week ago

This new on_zoom feature is useful for implementing zoom-dependent logic, e.g. adding or removing components to the map based on zoom level.

For example:

def handle_zoom(**kwargs):
    z = kwargs["zoom"]
    legend_on_map = legend_control.model_id in m._control_ids
    if (z>9):
        if not legend_on_map:
            m.add(legend_control)
    else:
        if legend_on_map:
            m.remove(legend_control)

m.on_zoom(handle_zoom)

where legend_control is a control that I want shown only on zoom 10 or above:

martinRenou commented 1 week ago

Thanks! I believe this was already possible using traitlets:

m = Map()

m.observe(handle_zoom, 'zoom')
lopezvoliver commented 6 days ago

Thanks! I believe this was already possible using traitlets:

m = Map()

m.observe(handle_zoom, 'zoom')

This works, I can access the 'new' zoom in my handle_zoom function. Thanks!