leaflet_documentation.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.leaflet)
  4. def main_demo() -> None:
  5. m = ui.leaflet(center=(51.505, -0.09))
  6. ui.label().bind_text_from(m, 'center', lambda center: f'Center: {center[0]:.3f}, {center[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_center((51.505, -0.090)))
  10. ui.button('Berlin', on_click=lambda: m.set_center((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(center=(51.505, -0.090), zoom=3)
  21. m.clear_layers()
  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="https://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 `center` 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(center=(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(latlng=(lat, lng))
  45. m.on('map-click', handle_click)
  46. @doc.demo('Move Markers', '''
  47. You can move markers with the `move` method.
  48. ''')
  49. def move_markers() -> None:
  50. m = ui.leaflet(center=(51.505, -0.09))
  51. marker = m.marker(latlng=m.center)
  52. ui.button('Move marker', on_click=lambda: marker.move(51.51, -0.09))
  53. @doc.demo('Vector Layers', '''
  54. Leaflet supports a set of [vector layers](https://leafletjs.com/reference.html#:~:text=VideoOverlay-,Vector%20Layers,-Path) like circle, polygon etc.
  55. These can be added with the `generic_layer` method.
  56. We are happy to review any pull requests to add more specific layers to simplify usage.
  57. ''')
  58. def vector_layers() -> None:
  59. m = ui.leaflet(center=(51.505, -0.09)).classes('h-32')
  60. m.generic_layer(name='circle', args=[m.center, {'color': 'red', 'radius': 300}])
  61. @doc.demo('Disable Pan and Zoom', '''
  62. There are [several options to configure the map in Leaflet](https://leafletjs.com/reference.html#map).
  63. This demo disables the pan and zoom controls.
  64. ''')
  65. def disable_pan_zoom() -> None:
  66. options = {
  67. 'zoomControl': False,
  68. 'scrollWheelZoom': False,
  69. 'doubleClickZoom': False,
  70. 'boxZoom': False,
  71. 'keyboard': False,
  72. 'dragging': False,
  73. }
  74. ui.leaflet(center=(51.505, -0.09), options=options)
  75. @doc.demo('Draw on Map', '''
  76. You can enable a toolbar to draw on the map.
  77. The `draw_control` can be used to configure the toolbar.
  78. This demo adds markers and polygons by clicking on the map.
  79. By setting "edit" and "remove" to `True` (the default), you can enable editing and deleting drawn shapes.
  80. ''')
  81. def draw_on_map() -> None:
  82. from nicegui import events
  83. def handle_draw(e: events.GenericEventArguments):
  84. layer_type = e.args['layerType']
  85. coords = e.args['layer'].get('_latlng') or e.args['layer'].get('_latlngs')
  86. ui.notify(f'Drawn a {layer_type} at {coords}')
  87. draw_control = {
  88. 'draw': {
  89. 'polygon': True,
  90. 'marker': True,
  91. 'circle': True,
  92. 'rectangle': True,
  93. 'polyline': True,
  94. 'circlemarker': True,
  95. },
  96. 'edit': {
  97. 'edit': True,
  98. 'remove': True,
  99. },
  100. }
  101. m = ui.leaflet(center=(51.505, -0.09), draw_control=draw_control)
  102. m.classes('h-96')
  103. m.on('draw:created', handle_draw)
  104. m.on('draw:edited', lambda: ui.notify('Edit completed'))
  105. m.on('draw:deleted', lambda: ui.notify('Delete completed'))
  106. @doc.demo('Draw with Custom Options', '''
  107. You can draw shapes with custom options like stroke color and weight.
  108. To hide the default rendering of drawn items, set `hide_drawn_items` to `True`.
  109. ''')
  110. def draw_custom_options():
  111. from nicegui import events
  112. def handle_draw(e: events.GenericEventArguments):
  113. options = {'color': 'red', 'weight': 1}
  114. m.generic_layer(name='polygon', args=[e.args['layer']['_latlngs'], options])
  115. draw_control = {
  116. 'draw': {
  117. 'polygon': True,
  118. 'marker': False,
  119. 'circle': False,
  120. 'rectangle': False,
  121. 'polyline': False,
  122. 'circlemarker': False,
  123. },
  124. 'edit': {
  125. 'edit': False,
  126. 'remove': False,
  127. },
  128. }
  129. m = ui.leaflet(center=(51.5, 0), draw_control=draw_control, hide_drawn_items=True)
  130. m.on('draw:created', handle_draw)
  131. @doc.demo('Run Map Methods', '''
  132. You can run methods of the Leaflet map object with `run_map_method`.
  133. This demo shows how to fit the map to the whole world.
  134. ''')
  135. def run_map_methods() -> None:
  136. m = ui.leaflet(center=(51.505, -0.09)).classes('h-32')
  137. ui.button('Fit world', on_click=lambda: m.run_map_method('fitWorld'))
  138. @doc.demo('Run Layer Methods', '''
  139. You can run methods of the Leaflet layer objects with `run_layer_method`.
  140. This demo shows how to change the opacity of a marker or change its icon.
  141. ''')
  142. def run_layer_methods() -> None:
  143. m = ui.leaflet(center=(51.505, -0.09)).classes('h-32')
  144. marker = m.marker(latlng=m.center)
  145. ui.button('Hide', on_click=lambda: marker.run_method('setOpacity', 0.3))
  146. ui.button('Show', on_click=lambda: marker.run_method('setOpacity', 1.0))
  147. icon = 'L.icon({iconUrl: "https://leafletjs.com/examples/custom-icons/leaf-green.png"})'
  148. ui.button('Change icon', on_click=lambda: marker.run_method(':setIcon', icon))
  149. @doc.demo('Wait for Initialization', '''
  150. You can wait for the map to be initialized with the `initialized` method.
  151. This is necessary when you want to run methods like fitting the bounds of the map right after the map is created.
  152. ''')
  153. async def wait_for_init() -> None:
  154. m = ui.leaflet(zoom=5)
  155. central_park = m.generic_layer(name='polygon', args=[[
  156. (40.767809, -73.981249),
  157. (40.800273, -73.958291),
  158. (40.797011, -73.949683),
  159. (40.764704, -73.973741),
  160. ]])
  161. await m.initialized()
  162. bounds = await central_park.run_method('getBounds')
  163. m.run_map_method('fitBounds', [[bounds['_southWest'], bounds['_northEast']]])
  164. doc.reference(ui.leaflet)