leaflet_documentation.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.
  15. You can find more map styles at <https://leaflet-extras.github.io/leaflet-providers/preview/>.
  16. Each call to `tile_layer` stacks upon the previous ones.
  17. So if you want to change the map style, you have to remove the default one first.
  18. ''')
  19. def map_style() -> None:
  20. m = ui.leaflet(location=(51.505, -0.090), zoom=3)
  21. del m.layers[0]
  22. m.tile_layer(
  23. url_template=r'https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png',
  24. options={
  25. 'maxZoom': 17,
  26. 'attribution':
  27. 'Map data: &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, <a href="http://viewfinderpanoramas.org">SRTM</a> | '
  28. 'Map style: &copy; <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)'
  29. },
  30. )
  31. @doc.demo('Add Markers on Click', '''
  32. You can add markers to the map with `marker`.
  33. The `location` argument is a tuple of latitude and longitude.
  34. This demo adds markers by clicking on the map.
  35. Note that the "map-click" event refers to the click event of the map object,
  36. while the "click" event refers to the click event of the container div.
  37. ''')
  38. def markers() -> None:
  39. from nicegui import events
  40. m = ui.leaflet(location=(51.505, -0.09))
  41. def handle_click(e: events.GenericEventArguments):
  42. lat = e.args['latlng']['lat']
  43. lng = e.args['latlng']['lng']
  44. m.marker(location=(lat, lng))
  45. m.on('map-click', handle_click)
  46. @doc.demo('Vector Layers', '''
  47. Leaflet supports a set of [vector layers](https://leafletjs.com/reference.html#:~:text=VideoOverlay-,Vector%20Layers,-Path) like circle, polygon etc.
  48. These can be added with the `generic_layer` method.
  49. We are happy to review any pull requests to add more specific layers to simplify usage.
  50. ''')
  51. def vector_layers() -> None:
  52. m = ui.leaflet(location=(51.505, -0.09)).classes('h-32')
  53. m.generic_layer(name='circle',
  54. args=[[51.505, -0.09], {'color': 'red', 'radius': 300}])
  55. @doc.demo('Draw on Map', '''
  56. You can enable a toolbar to draw on the map.
  57. The `draw_control` can be used to configure the toolbar.
  58. This demo adds markers and polygons by clicking on the map.
  59. ''')
  60. def draw_on_map() -> None:
  61. from nicegui import events
  62. def handle_draw(e: events.GenericEventArguments):
  63. if e.args['layerType'] == 'marker':
  64. m.marker(location=(e.args['layer']['_latlng']['lat'],
  65. e.args['layer']['_latlng']['lng']))
  66. if e.args['layerType'] == 'polygon':
  67. m.generic_layer(name='polygon', args=[e.args['layer']['_latlngs']])
  68. draw_control = {
  69. 'draw': {
  70. 'polygon': True,
  71. 'marker': True,
  72. 'circle': False,
  73. 'rectangle': False,
  74. 'polyline': False,
  75. 'circlemarker': False,
  76. },
  77. 'edit': False,
  78. }
  79. m = ui.leaflet(location=(51.505, -0.09), zoom=13, draw_control=draw_control)
  80. m.on('draw:created', handle_draw)
  81. doc.reference(ui.leaflet)