Class Geolocation
- All Implemented Interfaces:
Serializable
Two usage modes are available:
get(SerializableConsumer)for a one-shot position request with callbackstrack(Component)for continuous position tracking via a reactiveSignal, automatically tied to the owner component's lifecycle
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 Summary
Modifier and TypeMethodDescriptionstatic voidget(GeolocationOptions options, SerializableConsumer<GeolocationPosition> onSuccess) Requests the current position from the browser's Geolocation API with the given options.static voidget(GeolocationOptions options, SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError) Requests the current position from the browser's Geolocation API with the given options.static voidget(SerializableConsumer<GeolocationPosition> onSuccess) Requests the current position from the browser's Geolocation API.static voidget(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError) Requests the current position from the browser's Geolocation API.state()Returns a signal holding the current tracking state.static GeolocationStarts continuous position tracking, tied to the owner component's lifecycle.static Geolocationtrack(Component owner, GeolocationOptions options) Starts continuous position tracking with the given options, tied to the owner component's lifecycle.
-
Method Details
-
get
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, ornullfor browser defaultsonSuccess- 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 availableonError- called with aGeolocationErrorif the browser reports an error, ornullto 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, ornullfor browser defaultsonSuccess- called with the position when it becomes availableonError- called with aGeolocationErrorif the browser reports an error, ornullto ignore errors
-
track
Starts continuous position tracking, tied to the owner component's lifecycle.The tracking state is available through
state(), which starts asGeolocationState.Pendingand transitions toGeolocationPositionorGeolocationErroras the browser reports updates. Tracking stops automatically when the owner component detaches.- Parameters:
owner- the component whose lifecycle controls the tracking- Returns:
- a
Geolocationinstance with a reactive state signal
-
track
Starts continuous position tracking with the given options, tied to the owner component's lifecycle.The tracking state is available through
state(), which starts asGeolocationState.Pendingand transitions toGeolocationPositionorGeolocationErroras the browser reports updates. Tracking stops automatically when the owner component detaches.- Parameters:
owner- the component whose lifecycle controls the trackingoptions- the geolocation options, ornullfor browser defaults- Returns:
- a
Geolocationinstance with a reactive state signal
-
state
Returns a signal holding the current tracking state. The signal starts asGeolocationState.Pendingand transitions toGeolocationPositionorGeolocationErroras the browser reports updates.- Returns:
- a read-only signal with the current geolocation state
-