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

Unexpected behavoir with Leaflet set_center and set_zoom #2826

Closed
falkoschindler opened this issue Apr 5, 2024 Discussed in #2825 · 2 comments
Closed

Unexpected behavoir with Leaflet set_center and set_zoom #2826

falkoschindler opened this issue Apr 5, 2024 Discussed in #2825 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@falkoschindler
Copy link
Contributor

Discussed in #2825

Originally posted by thetableman April 5, 2024
Consider the below Leaflet example:

from nicegui import ui

async def reset_location():
    m.set_center(center=(51.50, -0.11))
    m.set_zoom(11)
    print(m.center)
    print(m.zoom)

with ui.row().classes('h-96 w-96'):
    ui.button('Reset location', on_click=reset_location)
    m = ui.leaflet(center=(51.50, -0.11), zoom=11)
    print(m.center)
    print(m.zoom)

ui.run()

When executed it displays as expected.
image

However, when you move the map and change the zoom to something that resembles this...
image

And then click Reset Location, the map doesn't display as expected, instead it just zooms in.
image

Sometimes if you click Reset Location again it resets as expected. Other times it doesn't seem to matter how often you click Reset Location, the map doesn't display the expected location.

You'll also see inconsistencies in the way m.center returns a value
image

@falkoschindler falkoschindler added the bug Something isn't working label Apr 5, 2024
@falkoschindler falkoschindler added this to the 1.4.21 milestone Apr 5, 2024
@falkoschindler
Copy link
Contributor Author

That's indeed unexpected!
It looks like adding self.update() in leaflet.py before calling self.run_method('setCenter', center) or self.run_method('setZoom', zoom) avoids this problem. This reminds me of PR #2779 where we experiment with a very similar solution.

@falkoschindler
Copy link
Contributor Author

Like in PR #2779 I switched to sending normal updates to the client and using the updated lifecycle hook instead of using run_method to update center and zoom.

Furthermore, I replaced panTo and setZoom with a single setView. Only then the bug disappeared reliably.

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

No branches or pull requests

1 participant