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

Leaflet element for interactive maps #1217

Merged
merged 44 commits into from
Dec 16, 2023
Merged

Leaflet element for interactive maps #1217

merged 44 commits into from
Dec 16, 2023

Conversation

falkoschindler
Copy link
Contributor

@falkoschindler falkoschindler commented Jul 21, 2023

I've started to implement an official Leaflet element for NiceGUI. It is quite an undertaking with a similar complexity like ui.scene.

So far we can use it like this:

m = ui.leaflet(location=(51.505, -0.09)).classes('w-96 h-64')
ui.label().bind_text_from(m, 'location', lambda location: f'Location: {location[0]}, {location[1]}')
ui.label().bind_text_from(m, 'zoom', lambda zoom: f'Zoom: {zoom}')

with ui.grid(columns=2):
    ui.button('London', on_click=lambda: m.set_location((51.505, -0.09)))
    ui.button('Berlin', on_click=lambda: m.set_location((52.520008, 13.404954)))
    ui.button('Zoom in', on_click=lambda: m.set_zoom(m.zoom + 1))
    ui.button('Zoom out', on_click=lambda: m.set_zoom(m.zoom - 1))
    ui.button('Add layer', on_click=lambda: m.tile_layer(url_template='https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png'))
    ui.button('Add marker', on_click=lambda: m.marker(location=(51.505, -0.09)).draggable())

The goal is allowing to interactively edit markers and geometric shapes like in this Leaflet demo:
https://leaflet.github.io/Leaflet.draw/docs/examples/full.html


Open tasks

  • fix multiple maps on same page
  • load js and css locally?
  • documentation
  • pytest
  • fix on air
  • use_draw_controls?
  • remove "map" example?
  • How to use draw controls? Demo?
  • rename location to latlng (marker) or center (map)
  • options parameter for initializer
  • update location and zoom via run_method (independent of options)

nicegui/elements/leaflet.js Outdated Show resolved Hide resolved
# Conflicts:
#	DEPENDENCIES.md
#	nicegui/ui.py
@falkoschindler falkoschindler added this to the 1.4.6 milestone Dec 8, 2023
@falkoschindler falkoschindler added the enhancement New feature or request label Dec 8, 2023
@falkoschindler
Copy link
Contributor Author

This PR is finally taking shape. The basic functionality is there, but the map is not visible when accessed via NiceGUI On Air.
Apart from that we should decide if the current feature set is enough for a release or is some crucial functions are missing.

By the way: I introduced yet another mechanism for adding JavaScript libraries. By calling add_resource() in Python and loadResource() in JavaScript one can easily add any CSS or JS file to the HTML header. This should be useful for other elements as well, like ui.fullcalendar in PR #2054.

@falkoschindler falkoschindler marked this pull request as ready for review December 8, 2023 13:16
@rodja
Copy link
Member

rodja commented Dec 15, 2023

I've split the demos for using other map styles and adding markers to gain more clarity for each demo. I've noticed two things:

  • The default tile_layer should be removed before adding a new one. Otherwise leaflet will load the tiles of both layers.
  • The 'click' event args do not have a latlog field which currently breaks the marker demo which I changed to add markers at the click location.

@falkoschindler
Copy link
Contributor Author

@rodja I just finished the demo for draw_control.
Note that I didn't change the name, because it can also be a dictionary configuring the toolbar.

From my point of view this PR is ready to merge. Maybe you can check the demos once again?

@falkoschindler falkoschindler requested a review from rodja December 15, 2023 19:53
Copy link
Member

@rodja rodja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was about to merge this but then discovered that the zoom in/out buttons of the first demo are not working at all.

@falkoschindler
Copy link
Contributor Author

@rodja Thanks for spotting the zoom bug. Besides a type there have been even more bugs potentially causing endless event loops. But it should be fixed now.

Besides that I added some more options to the disable_pan_zoom demo. Otherwise you can scroll via double click, using the scroll wheel, or dragging a zoom box while holding shift.

@falkoschindler falkoschindler requested a review from rodja December 16, 2023 19:53
@rodja rodja merged commit fd3d2ab into main Dec 16, 2023
6 checks passed
@rodja rodja deleted the leaflet branch December 16, 2023 20:31
Copy link
Member

@rodja rodja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants