Class LeafletMap

java.lang.Object
com.vaadin.flow.component.Component
org.vaadin.addons.componentfactory.leaflet.LeafletMap
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, com.vaadin.flow.component.HasTheme, Serializable, Evented, SupportsKeyboardEvents, SupportsLayerEvents, SupportsLocationEvents, SupportsMapEvents, SupportsMouseEvents, Identifiable, ExecutableFunctions, GeolocationFunctions, MapConversionFunctions, MapGetStateFunctions, MapModifyStateFunctions

@Tag("leaflet-map") @NpmPackage(value="leaflet", version="1.9.4") @JsModule("./leaflet-map.js") @JsModule("leaflet/dist/leaflet-src.js") @CssImport(value="leaflet/dist/leaflet.css",themeFor="leaflet-map") @CssImport(value="./styles/leaflet-lumo-theme.css",themeFor="leaflet-map") public final class LeafletMap extends com.vaadin.flow.component.Component implements MapModifyStateFunctions, MapGetStateFunctions, GeolocationFunctions, MapConversionFunctions, SupportsMouseEvents, SupportsMapEvents, SupportsLocationEvents, SupportsKeyboardEvents, SupportsLayerEvents, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasTheme, com.vaadin.flow.component.HasStyle
Server-side component wrapping the Leaflet interactive map. It manages the map layers, controls, options and events, and executes the corresponding function calls on the client-side Leaflet instance.
See Also:
  • Constructor Details

    • LeafletMap

      public LeafletMap()
      Creates a new map using the default map options.
    • LeafletMap

      public LeafletMap(MapOptions mapOptions)
      Creates a new map configured with the given map options.
      Parameters:
      mapOptions - the options used to initialize the map
  • Method Details

    • onAttach

      protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
      Sends the registered event types to the client when the component is attached, so that the matching Leaflet listeners get bound.
      Overrides:
      onAttach in class com.vaadin.flow.component.Component
      Parameters:
      attachEvent - the attach event
    • getLayer

      public Layer getLayer(String layerId)
      Returns the layer with the given internal ID.
      Parameters:
      layerId - the id of the layer to be looking for
      Returns:
      the layer with the given internal ID
    • findLayer

      public Layer findLayer(String layerId)
      Recursively searches the map, including nested layer groups, for the layer with the given internal ID.
      Parameters:
      layerId - the id of the layer to look for
      Returns:
      the matching layer, or the root map layer if none is found
    • addLayer

      public void addLayer(Layer layer)
      Adds the given layer to the map
      Parameters:
      layer - the layer to add
    • removeLayer

      public void removeLayer(Layer layer)
      Removes the given layer from the map.
      Parameters:
      layer - the layer to remove
    • setBaseUrl

      public void setBaseUrl(String baseUrl)
      Adds a tile layer to the map using the given URL template as the tile source.
      Parameters:
      baseUrl - the tile layer URL template
    • addControl

      public void addControl(LeafletControl leafletControl)
      Adds the given control to the map
      Parameters:
      leafletControl - the control to add
    • removeControl

      public void removeControl(LeafletControl leafletControl)
      Removes the given control from the map
      Parameters:
      leafletControl - the control to remove
    • whenReady

      public com.vaadin.flow.shared.Registration whenReady(com.vaadin.flow.component.ComponentEventListener<LeafletMap.MapReadyEvent> listener)
      Fired when the map gets initialized with a view (center and zoom) and at least one layer, or immediately if it's already initialized.
      Parameters:
      listener - the listener to call when the event occurs, not null
      Returns:
      a handle that can be used for removing the listener
    • addThemeVariants

      public void addThemeVariants(LeafletMapVariant... variants)
      Adds theme variants to the map component.
      Parameters:
      variants - theme variants to add
    • executeJs

      public void executeJs(Identifiable target, String functionName, Serializable... arguments)
      Executes the named Leaflet function on the client for the given target, without expecting a return value.
      Specified by:
      executeJs in interface ExecutableFunctions
      Parameters:
      target - the client object (map, layer or control) to invoke the function on
      functionName - the name of the Leaflet function to call
      arguments - the arguments passed to the function
    • call

      public <T extends Serializable> CompletableFuture<T> call(Identifiable target, String functionName, Class<T> resultType, Serializable... arguments)
      Calls the named Leaflet function on the client for the given target and asynchronously returns its result deserialized to the requested type.
      Specified by:
      call in interface ExecutableFunctions
      Type Parameters:
      T - the type of the returned result
      Parameters:
      target - the client object (map, layer or control) to invoke the function on
      functionName - the name of the Leaflet function to call
      resultType - the type the client result should be deserialized to
      arguments - the arguments passed to the function
      Returns:
      a future completed with the function result, or null if the map is not ready yet
    • addEventListener

      public <T extends LeafletEvent> void addEventListener(LeafletEventType eventType, LeafletEventListener<T> listener)
      Registers a listener for the given Leaflet event type on the map.
      Specified by:
      addEventListener in interface Evented
      Type Parameters:
      T - the type of the fired event
      Parameters:
      eventType - the event type to listen for
      listener - the listener invoked when the event occurs
    • getUuid

      public String getUuid()
      Returns the unique identifier of the map.
      Specified by:
      getUuid in interface Identifiable
      Returns:
      the map uuid
    • removeEventListener

      public void removeEventListener(LeafletEventType eventType)
      Removes the listeners registered for the given Leaflet event type.
      Specified by:
      removeEventListener in interface Evented
      Parameters:
      eventType - the event type to stop listening for
    • clearAllEventListeners

      public void clearAllEventListeners()
      Removes all event listeners registered on the map.
      Specified by:
      clearAllEventListeners in interface Evented
    • hasEventListeners

      public boolean hasEventListeners(LeafletEventType eventType)
      Returns whether any listener is registered for the given Leaflet event type.
      Specified by:
      hasEventListeners in interface Evented
      Parameters:
      eventType - the event type to check
      Returns:
      true if at least one listener is registered for the event type
    • isReady

      public boolean isReady()
      Returns whether the map has been initialized on the client side.
      Returns:
      the ready