Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent leaflet from resetting center and zoom when browser resizes #4265

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

falkoschindler
Copy link
Contributor

This PR tries to solve #4182 by changing the update mechanism for center and zoom yet again.

As it turned out, Vue's updated() lifecycle hook is not only called when a prop is updated from the server, but also when the window resizes (vertically). In this case the "center" and "zoom" props can be outdated if the user navigated the map manually. But the props are used to call setView, causing the map to reset to the last location sent from the server.

Various test setups from this issue as well as previous issues seem to work just right:

l = ui.leaflet(zoom=5)
ui.button('set center', on_click=lambda: l.set_center((48.1373, 11.5755)))
@ui.page('/')
async def page():
    m = ui.leaflet(zoom=5)
    central_park = m.generic_layer(name='polygon', args=[[
        (40.767809, -73.981249),
        (40.800273, -73.958291),
        (40.797011, -73.949683),
        (40.764704, -73.973741),
    ]])
    await m.initialized()
    bounds = await central_park.run_method('getBounds')
    m.run_map_method('fitBounds', [[bounds['_southWest'], bounds['_northEast']]])
m = ui.leaflet(center=(48.1, 11.6), zoom=10)
m.on('map-moveend', lambda e: print(e.args['center'], e.args['zoom']))
ui.button('Berlin', on_click=lambda: m.run_map_method('flyTo', [52.5, 13.4], 9, {'duration': 1.0}))

@falkoschindler falkoschindler added the bug Something isn't working label Jan 24, 2025
@falkoschindler falkoschindler added this to the 2.11 milestone Jan 24, 2025
@falkoschindler falkoschindler linked an issue Jan 24, 2025 that may be closed by this pull request
@falkoschindler falkoschindler merged commit 14a3488 into main Jan 24, 2025
9 checks passed
@falkoschindler falkoschindler deleted the leaflet-reset branch January 24, 2025 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ui.leaflet location is reset when ui.plotly is updated
2 participants