Class Geolocation
- All Implemented Interfaces:
Serializable
getPosition(SerializableConsumer, SerializableConsumer)— read the user's location once.watchPosition(Component)— keep receiving updates as the user moves; returns aGeolocationWatcherhandle that exposes both a listener API and a reactiveSignal.
GeolocationError
with code GeolocationErrorCode.PERMISSION_DENIED.
One-shot example:
Button locate = new Button("Use my location");
locate.addClickListener(
e -> Geolocation.getPosition(
pos -> showNearest(pos.coords().latitude(),
pos.coords().longitude()),
err -> showManualEntry()));
Continuous tracking with a listener (data-flow style):
GeolocationWatcher watcher = Geolocation.watchPosition(this);
watcher.addPositionListener(pos -> repository.save(pos), err -> LOGGER
.warn("location error: {} ({})", err.errorCode(), err.debugInfo()));
Continuous tracking with a signal (reactive UI style):
GeolocationWatcher watcher = Geolocation.watchPosition(this);
Signal<GeolocationResult> signal = watcher.positionSignal();
status.bindText(signal.map(result -> switch (result) {
case GeolocationPending p -> "Waiting for first reading…";
case GeolocationError err -> "Could not locate you.";
default -> "";
}));
Signal.effect(map, () -> {
if (signal.get() instanceof GeolocationPosition pos) {
map.setCenter(new Coordinate(pos.coords().longitude(),
pos.coords().latitude()));
}
});
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Signal<GeolocationAvailability> Returns a read-only signal hinting at whether geolocation is usable for the current UI.static Signal<GeolocationAvailability> Returns a read-only signal hinting at whether geolocation is usable for the given UI.static voidgetPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError) Requests the user's current position once, using the current UI.static voidgetPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError, GeolocationOptions options) Requests the user's current position once, using the current UI, with tuning options.static voidgetPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError, GeolocationOptions options, UI ui) Requests the user's current position once on the given UI with tuning options.static voidgetPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError, UI ui) Requests the user's current position once on the given UI.static GeolocationWatcherwatchPosition(Component owner) Starts continuously watching the user's position, tied to the owner component's lifecycle.static GeolocationWatcherwatchPosition(Component owner, GeolocationOptions options) Starts continuously watching the user's position with tuning options, tied to the owner component's lifecycle.
-
Method Details
-
getPosition
public static void getPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError) Requests the user's current position once, using the current UI.- Parameters:
onSuccess- invoked when the browser reports a position, nevernullonError- invoked when the browser reports an error, nevernull- Throws:
NullPointerException- if either consumer isnullIllegalStateException- if there is no current UI
-
getPosition
public static void getPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError, GeolocationOptions options) Requests the user's current position once, using the current UI, with tuning options. SeeGeolocationOptionsfor the available settings.- Parameters:
onSuccess- invoked when the browser reports a position, nevernullonError- invoked when the browser reports an error, nevernulloptions- accuracy / timeout / cache-age tuning, nevernull; pass an empty instance viaGeolocationOptions.builder().build()to use browser defaults- Throws:
NullPointerException- if any argument isnullIllegalStateException- if there is no current UI
-
getPosition
public static void getPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError, UI ui) Requests the user's current position once on the given UI. Use this overload from background threads or anywhereUI.getCurrent()is unreliable.- Parameters:
onSuccess- invoked when the browser reports a position, nevernullonError- invoked when the browser reports an error, nevernullui- the UI to dispatch the request through, nevernull- Throws:
NullPointerException- if any argument isnull
-
getPosition
public static void getPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError, GeolocationOptions options, UI ui) Requests the user's current position once on the given UI with tuning options. Use this overload from background threads or anywhereUI.getCurrent()is unreliable.- Parameters:
onSuccess- invoked when the browser reports a position, nevernullonError- invoked when the browser reports an error, nevernulloptions- accuracy / timeout / cache-age tuning, nevernull; pass an empty instance viaGeolocationOptions.builder().build()to use browser defaultsui- the UI to dispatch the request through, nevernull- Throws:
NullPointerException- if any argument isnull
-
watchPosition
Starts continuously watching the user's position, tied to the owner component's lifecycle. The browser reports new positions as the device moves; consume them viaGeolocationWatcher.addPositionListener(SerializableConsumer, SerializableConsumer)for callbacks orGeolocationWatcher.positionSignal()for a reactive signal. The watch is cancelled automatically whenownerdetaches; callGeolocationWatcher.stop()to cancel sooner andGeolocationWatcher.resume()to resume.The watch starts as soon as
owneris attached to a UI, so this method is safe to call from a view constructor: if the component is already attached the watch starts immediately, otherwise it starts on first attach.Permission-revoke caveat. If the user revokes geolocation permission while a watch is active and then grants it again, the browser silently stops delivering updates to the existing watch — this is the W3C Geolocation API's documented behavior across browsers, not a Flow-specific limitation. Recover by calling
GeolocationWatcher.stop()followed byGeolocationWatcher.resume()to install a fresh browser watch; subscribing toavailabilityHintSignal()can drive that automatically.- Parameters:
owner- the component whose detach cancels the watch; the watch activates on the owner's first attach- Returns:
- a watcher exposing the position stream and a stop/resume handle
- Throws:
NullPointerException- ifownerisnull
-
watchPosition
Starts continuously watching the user's position with tuning options, tied to the owner component's lifecycle. Behaves likewatchPosition(Component)but lets the caller request high accuracy, set a failure timeout, or accept cached readings. SeeGeolocationOptionsfor the available settings.- Parameters:
owner- the component whose detach cancels the watch; the watch activates on the owner's first attachoptions- accuracy / timeout / cache-age tuning, nevernull; pass an empty instance viaGeolocationOptions.builder().build()to use browser defaults- Returns:
- a watcher exposing the position stream and a stop/resume handle
- Throws:
NullPointerException- if either argument isnull
-
availabilityHintSignal
Returns a read-only signal hinting at whether geolocation is usable for the current UI. Useful for pre-rendering decisions like hiding a "Locate me" button on insecure contexts or auto-fetching on return visits when permission is already granted.The value is best-effort and can be briefly stale:
- Safari does not report permission state — every state surfaces as
UNKNOWN(exceptUNSUPPORTED, which is always reported correctly). - Firefox does not reliably propagate permission changes the user makes
in browser settings; the value can stay stale until the next
getPosition(com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.geolocation.GeolocationPosition>, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.geolocation.GeolocationError>)orwatchPosition(com.vaadin.flow.component.Component)call. - Chromium has a small propagation delay between the browser permission event and the cache update.
getPosition(com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.geolocation.GeolocationPosition>, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.geolocation.GeolocationError>)and inspect the outcome.- Returns:
- the availability hint signal
- Throws:
IllegalStateException- if there is no current UI
- Safari does not report permission state — every state surfaces as
-
availabilityHintSignal
Returns a read-only signal hinting at whether geolocation is usable for the given UI. Same semantics asavailabilityHintSignal(); use this overload from background threads or anywhereUI.getCurrent()is unreliable.- Parameters:
ui- the UI to read the hint from, nevernull- Returns:
- the availability hint signal
- Throws:
NullPointerException- ifuiisnull
-