Class GeolocationTracker

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

public class GeolocationTracker extends Object implements Serializable
A handle to a geolocation tracking session, returned by Geolocation.track(Component) / Geolocation.track(Component, GeolocationOptions).

Exposes the latest GeolocationResult as a reactive signal via valueSignal(), and lets the application cancel tracking via stop() or resume it via resume(). The underlying browser watch is also cancelled automatically when the owning component detaches, so most applications never need to call stop() explicitly — it is provided for "Stop tracking" buttons and similar mid-view cancellation.

A tracker is reusable: after stop() you can call resume() to resume tracking on the same handle, and any effects or bindings subscribed to valueSignal() continue to work. Bind a toggle button's state to activeSignal() to let the UI react to start/stop without tracking your own flag.

See Also:
  • Method Details

    • valueSignal

      public Signal<GeolocationResult> valueSignal()
      Returns a read-only signal that holds the most recent tracking result.

      Combine with Signal.effect(owner, ...) or an attach listener to run code whenever the value changes — the effect re-runs automatically on every update and no manual event-listener bookkeeping is required. Inside an effect or another reactive context, call valueSignal().get() to read the current value and subscribe to further updates; outside a reactive context, call valueSignal().peek() to read a snapshot without subscribing.

      The signal starts as GeolocationPending until the first reading arrives, then transitions to GeolocationPosition on every successful reading, or GeolocationError on failure. After stop() (or after the owner detaches), the last value remains readable but the signal stops receiving updates. Calling resume() resumes updates; the signal is reset to GeolocationPending on resume.

      Returns:
      a read-only signal reporting the latest result
    • activeSignal

      public Signal<Boolean> activeSignal()
      Returns a read-only signal that indicates whether the tracker is currently receiving updates. Flips to true on resume() and to false on stop() (or when the owner detaches).

      Subscribe with Signal.effect(owner, ...) to bind a toggle button's label/state to the tracker without tracking a separate flag. Inside a reactive context, call activeSignal().get() to subscribe; outside a reactive context, call activeSignal().peek() for a snapshot.

      Returns:
      a read-only signal reporting whether tracking is active
    • resume

      public void resume()
      Starts, or resumes, the underlying browser watch.

      Called automatically from the constructor so that a freshly created tracker is immediately active. Call again after stop() to resume tracking on the same handle — any effects or bindings subscribed to valueSignal() stay attached and start receiving new updates.

      The signal is reset to GeolocationPending on every resume. Calling resume() on an already-running tracker is a no-op.

    • stop

      public void stop()
      Cancels the underlying browser watch and tears down the server-side listeners.

      The browser stops reporting position updates and valueSignal() stops changing. The last value remains readable. This is the way to end tracking from application code (e.g. a "Stop" button) — leaving the view automatically calls this method, so there is no need to call it from a detach listener.

      Idempotent and always safe: calling it twice, or calling it on a tracker whose owner has already detached, does nothing extra. After stop() the tracker can be resumed with resume().