Class Geolocation

java.lang.Object
com.vaadin.flow.component.geolocation.Geolocation
All Implemented Interfaces:
Serializable

public class Geolocation extends Object implements Serializable
Provides access to the browser's Geolocation API.

Two usage modes are available:

One-shot example:

 Geolocation.get(pos -> map.setCenter(pos.coords().latitude(),
         pos.coords().longitude()));
 

Tracking example:

 Geolocation geo = Geolocation.track(this);
 ComponentEffect.effect(this, () -> {
     switch (geo.state().get()) {
     case GeolocationState.Pending p -> {
     }
     case GeolocationPosition pos ->
         map.setCenter(pos.coords().latitude(), pos.coords().longitude());
     case GeolocationError err -> showError(err.message());
     }
 });
 
See Also:
  • Method Details

    • get

      public static void get(SerializableConsumer<GeolocationPosition> onSuccess)
      Requests the current position from the browser's Geolocation API. Errors are silently ignored.

      Must be called from a UI thread (i.e., inside a request handler or event listener). The callback runs in the UI thread, so there is no need to use ui.access().

      Parameters:
      onSuccess - called with the position when it becomes available
    • get

      public static void get(GeolocationOptions options, SerializableConsumer<GeolocationPosition> onSuccess)
      Requests the current position from the browser's Geolocation API with the given options. Errors are silently ignored.

      Must be called from a UI thread (i.e., inside a request handler or event listener). The callback runs in the UI thread, so there is no need to use ui.access().

      Parameters:
      options - the geolocation options, or null for browser defaults
      onSuccess - called with the position when it becomes available
    • get

      public static void get(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError)
      Requests the current position from the browser's Geolocation API.

      Must be called from a UI thread (i.e., inside a request handler or event listener). Callbacks run in the UI thread, so there is no need to use ui.access().

      Parameters:
      onSuccess - called with the position when it becomes available
      onError - called with a GeolocationError if the browser reports an error, or null to ignore errors
    • get

      public static void get(GeolocationOptions options, SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError)
      Requests the current position from the browser's Geolocation API with the given options.

      Must be called from a UI thread (i.e., inside a request handler or event listener). Callbacks run in the UI thread, so there is no need to use ui.access().

      Parameters:
      options - the geolocation options, or null for browser defaults
      onSuccess - called with the position when it becomes available
      onError - called with a GeolocationError if the browser reports an error, or null to ignore errors
    • track

      public static Geolocation track(Component owner)
      Starts continuous position tracking, tied to the owner component's lifecycle.

      The tracking state is available through state(), which starts as GeolocationState.Pending and transitions to GeolocationPosition or GeolocationError as the browser reports updates. Tracking stops automatically when the owner component detaches.

      Parameters:
      owner - the component whose lifecycle controls the tracking
      Returns:
      a Geolocation instance with a reactive state signal
    • track

      public static Geolocation track(Component owner, GeolocationOptions options)
      Starts continuous position tracking with the given options, tied to the owner component's lifecycle.

      The tracking state is available through state(), which starts as GeolocationState.Pending and transitions to GeolocationPosition or GeolocationError as the browser reports updates. Tracking stops automatically when the owner component detaches.

      Parameters:
      owner - the component whose lifecycle controls the tracking
      options - the geolocation options, or null for browser defaults
      Returns:
      a Geolocation instance with a reactive state signal
    • state

      public Signal<GeolocationState> state()
      Returns a signal holding the current tracking state. The signal starts as GeolocationState.Pending and transitions to GeolocationPosition or GeolocationError as the browser reports updates.
      Returns:
      a read-only signal with the current geolocation state