Forráskód Böngészése

introduce leaflet.run_map_method

Falko Schindler 1 éve
szülő
commit
ba0e709cf9

+ 3 - 0
nicegui/elements/leaflet.js

@@ -111,5 +111,8 @@ export default {
     add_layer(layer) {
       L[layer.type](...layer.args).addTo(this.map);
     },
+    run_map_method(name, ...args) {
+      return this.map[name](...args);
+    },
   },
 };

+ 17 - 0
nicegui/elements/leaflet.py

@@ -99,6 +99,23 @@ class Leaflet(Element, component='leaflet.js'):
         self._props['zoom'] = zoom
         self.run_method('setZoom', zoom)
 
+    def run_map_method(self, name: str, *args, timeout: float = 1, check_interval: float = 0.01) -> AwaitableResponse:
+        """Run a method of the Leaflet map instance.
+
+        Refer to the `Leaflet documentation <https://leafletjs.com/reference.html#map-methods-for-modifying-map-state>`_ for a list of methods.
+
+        If the function is awaited, the result of the method call is returned.
+        Otherwise, the method is executed without waiting for a response.
+
+        :param name: name of the method
+        :param args: arguments to pass to the method
+        :param timeout: timeout in seconds (default: 1 second)
+        :param check_interval: interval in seconds to check for a response (default: 0.01 seconds)
+
+        :return: AwaitableResponse that can be awaited to get the result of the method call
+        """
+        return self.run_method('run_map_method', name, *args, timeout=timeout, check_interval=check_interval)
+
     def _handle_delete(self) -> None:
         binding.remove(self.layers, Layer)
         super().delete()

+ 9 - 0
website/documentation/content/leaflet_documentation.py

@@ -112,4 +112,13 @@ def draw_on_map() -> None:
     m.on('draw:created', handle_draw)
 
 
+@doc.demo('Run Map Methods', '''
+    You can run methods of the Leaflet map object with `run_map_method`.
+    This demo shows how to fit the map to the whole world.
+''')
+def run_map_methods() -> None:
+    m = ui.leaflet(center=(51.505, -0.09)).classes('h-32')
+    ui.button('Fit world', on_click=lambda: m.run_map_method('fitWorld'))
+
+
 doc.reference(ui.leaflet)