leaflet_documentation.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.leaflet)
  4. def main_demo() -> None:
  5. m = ui.leaflet(location=(51.505, -0.09))
  6. ui.label().bind_text_from(m, 'location', lambda location: f'Location: {location[0]:.3f}, {location[1]:.3f}')
  7. ui.label().bind_text_from(m, 'zoom', lambda zoom: f'Zoom: {zoom}')
  8. with ui.grid(columns=2):
  9. ui.button('London', on_click=lambda: m.set_location((51.505, -0.090)))
  10. ui.button('Berlin', on_click=lambda: m.set_location((52.520, 13.405)))
  11. ui.button(icon='zoom_in', on_click=lambda: m.set_zoom(m.zoom + 1))
  12. ui.button(icon='zoom_out', on_click=lambda: m.set_zoom(m.zoom - 1))
  13. @doc.demo('Changing the Map Style', '''
  14. The default map style is OpenStreetMap. You can find more map styles at <https://leaflet-extras.github.io/leaflet-providers/preview/>.
  15. Each call to `tile_layer` stacks upon the previous ones. So if you want to change the map style, you have to remove the default one first.
  16. ''')
  17. def map_style() -> None:
  18. m = ui.leaflet(location=(51.505, -0.090), zoom=3)
  19. del m.layers[0]
  20. m.tile_layer(url_template=r'https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png',
  21. options={
  22. 'maxZoom': 17,
  23. 'attribution': 'Map data: &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: &copy; <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)'
  24. })
  25. @doc.demo('Add markers', '''
  26. ''')
  27. def markers() -> None:
  28. m = ui.leaflet(location=(51.505, -0.09)).classes('h-32')
  29. ui.button(icon='pin_drop', on_click=lambda: m.marker(location=m.location))
  30. doc.reference(ui.leaflet)