-
-
Notifications
You must be signed in to change notification settings - Fork 769
Add a new method Leaflet.set_view #4496
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
base: main
Are you sure you want to change the base?
Conversation
…m, or both together. Added tests.
Wait. I think the issue is deeper than what you think. Checking both
This means that Also, your |
Thank you for contributing this pull request, @amks1! The center and zoom methods of
At least three times we tried to fix some weird behavior related to changing the map's center or zoom. So before simply adding another method I suggest to keep this PR here for reference and continue discussing the problem over at #4491. |
I did spend some time trying to figure out whether I could achieve this without adding another function, but didn't arrive at anything. I suspect it's something "deeper" in the code in the way javascript code is sequenced to execute on the client. The second command always overrides the first.
My code doesn't commit But what this means is these event handlers are handling simultaneous Code: @ui.page('/test')
def test_page():
LONDON = (51.555981, -0.046957)
ATHENS = (38.139204, 23.612089)
def set_marker_zoom_in():
marker = leaflet.marker(latlng=ATHENS)
leaflet.set_center(ATHENS)
leaflet.set_zoom(13)
def set_marker():
marker = leaflet.marker(latlng=ATHENS)
leaflet.set_center(ATHENS)
def set_center():
leaflet.set_center(ATHENS)
def set_center_zoom_in():
# leaflet.set_center(ATHENS)
# leaflet.set_zoom(13)
leaflet.set_view(ATHENS, 13)
leaflet = ui.leaflet(LONDON, zoom=6).style('height: 400px;')
ui.button('Set Marker and Zoom In', on_click=set_marker_zoom_in)
ui.button('Set Marker', on_click=set_marker)
ui.button('Only Set Center', on_click=set_center)
ui.button('Set center and zoom in', on_click=set_center_zoom_in)
ui.button('Show center and zoom', on_click=lambda: ui.notify(f'Center: {leaflet.center}, Zoom: {leaflet.zoom}'))
ui.run(reload=False, show=False) |
@amks1 Sorry to keep you waiting. It's quite mind-bending, but after reviewing the code some more, I think I got it.
nicegui/nicegui/elements/leaflet.py Lines 96 to 104 in 847c4ab
nicegui/nicegui/elements/leaflet.py Lines 111 to 125 in 847c4ab
Now, I think you will agree, that I can change the code slightly for easier readability: def set_center(self, center: Tuple[float, float]) -> None:
"""Set the center location of the map."""
if self._props['center'] == center:
return
self._props['center'] = center
if self._send_update_on_value_change:
self.run_map_method('setView', self._props['center'], self.zoom) # center = self._props['center']
def set_zoom(self, zoom: int) -> None:
"""Set the zoom level of the map."""
if self._props['zoom'] == zoom:
return
self._props['zoom'] = zoom
if self._send_update_on_value_change:
self.run_map_method('setView', self.center, self._props['zoom']) # zoom = self._props['zoom'] This makes it obvious: When you do two consecutive So, indeed, there is no way with existing functions that a zoom-and-move maneuver can be done in one fell swoop. Do @ me and correct me, if you think I am wrong, though. Given the above, and how I had to modify the code to make it readable, I take issue with how you do |
Let's see if this logic is more nice: def set_view(self, *, center: Optional[Tuple[float, float]] = None, zoom: Optional[int] = None) -> None:
"""Set the view of the map.
:param center: new center location (latitude/longitude)
:param zoom: new zoom level
"""
value_changed = False
if center is not None and self._props['center'] != center:
self._props['center'] = center
value_changed = True
if zoom is not None and self._props['zoom'] != zoom:
self._props['zoom'] = zoom
value_changed = True
if value_changed and self._send_update_on_value_change:
self.run_map_method('setView', center or self.center, zoom or self.zoom) Key points:
@falkoschindler code review time. See if this is the better solution. |
Checked for upstream changes in response to potential Leaflet 2.0 release. TL-DR: They were trying to use more modern syntax. This PR should remain safe to merge and its code should be working, whether Leaflet 2.0 came out or not. ![]() Use nullish coalescing and optional chaining (#9661)
Use nullish coalescing and optional chaining (#9661)![]() Also should be fine. |
Added the
set_view
method to set the center and zoom in one operation, because callingset_zoom
andset_center
one after the other was running only the latter command.Resolves #4491