Class Geolocation
- All Implemented Interfaces:
Serializable
UI.getGeolocation().
Every entry point on this class is asynchronous: calling it enqueues a request to the browser and returns immediately. The browser answers later (after the user responds to a permission prompt, after the operating system reports a position, or after a timeout), and Flow invokes the callback or updates the signal on the UI thread.
Two usage modes:
getPosition(SerializableConsumer, SerializableConsumer)— one-shot position request. Use this when the application only needs to know the user's location at a single moment (e.g. on a button click). Takes a pair of callbacks — one for a successfulGeolocationPosition, one for aGeolocationError— mirroring the W3CgetCurrentPosition(success, error)pair and matchingGeolocationWatcher.addPositionListener. An overload accepts a trailingGeolocationOptionsfor accuracy / timeout / cache-age tuning.watchPosition(Component)— continuous watching that keeps the server updated as the user moves. Returns aGeolocationWatcherwhosevalueSignal()is a reactive signal ofGeolocationResult. The browser watch is automatically cancelled when the owning component detaches; useGeolocationWatcher.stop()to cancel it sooner andGeolocationWatcher.resume()to resume.
availabilityHintSignal()— best-effort hint about whether the feature is usable and what permission state the origin has.
Permission prompts. The first time the application asks for a
location, the browser shows its own permission dialog. The dialog is
controlled by the browser, not by Flow — Flow cannot style it, suppress it,
or detect when it is shown. If the user denies the prompt the callback
receives a GeolocationError whose errorCode is GeolocationErrorCode.PERMISSION_DENIED.
One-shot example:
Button locate = new Button("Use my location");
locate.addClickListener(
e -> e.getUI().getGeolocation().getPosition(
pos -> showNearest(pos.coords().latitude(),
pos.coords().longitude()),
err -> showManualEntry()));
Watching example:
GeolocationWatcher watcher = UI.getCurrent().getGeolocation()
.watchPosition(this);
Signal.effect(this, () -> {
switch (watcher.valueSignal().get()) {
case GeolocationPending p -> {
// waiting for first reading
}
case GeolocationPosition pos ->
map.setCenter(new Coordinate(pos.coords().longitude(),
pos.coords().latitude()));
case GeolocationError err -> showError(err.message());
}
});
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns a read-only signal hinting at whether geolocation is usable for this UI.voidgetPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError) Requests the user's current position once.voidgetPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError, @Nullable GeolocationOptions options) Requests the user's current position once with tuning options.watchPosition(Component owner) Starts continuously watching the user's position, tied to the owner component's lifecycle.watchPosition(Component owner, @Nullable GeolocationOptions options) Starts continuously watching the user's position with tuning options, tied to the owner component's lifecycle.
-
Constructor Details
-
Geolocation
Creates a new Geolocation facade bound to the given UI.Framework-only. Application code obtains the instance via
UI.getGeolocation()and should not instantiate this class directly — attempting to create a second instance for a UI that already has one throws.The underlying
GeolocationClientis resolved throughLookup: if aGeolocationClientFactoryis registered, itsGeolocationClientFactory.create(UI)produces the client. Otherwise the built-in browser-backed client is used.- Parameters:
ui- the UI this facade belongs to- Throws:
IllegalStateException- if the UI already has a Geolocation facade
-
-
Method Details
-
getPosition
public void getPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError) Requests the user's current position once. On a successful readingonSuccessis invoked with theGeolocationPosition; if the browser reports an error insteadonErroris invoked with theGeolocationError. The pair mirrors the W3CgetCurrentPosition(success, error)signature and matchesGeolocationWatcher.addPositionListener, so callers can share the same handler shape between one-shot and watch APIs.The call returns immediately. The browser may show a permission dialog on the first call; after the user responds, exactly one of the callbacks is invoked on the UI thread.
- Parameters:
onSuccess- invoked with the position on a successful reading; notnullonError- invoked with the error if the browser reports one; notnull
-
getPosition
public void getPosition(SerializableConsumer<GeolocationPosition> onSuccess, SerializableConsumer<GeolocationError> onError, @Nullable GeolocationOptions options) Requests the user's current position once with tuning options. Use this to trade accuracy for battery/speed or to accept a recent cached reading. SeeGeolocationOptionsfor the available settings.The call returns immediately. The browser may show a permission dialog on the first call; after the user responds, exactly one of the callbacks is invoked on the UI thread.
- Parameters:
onSuccess- invoked with the position on a successful reading; notnullonError- invoked with the error if the browser reports one; notnulloptions- accuracy / timeout / cache-age tuning, ornullto use the browser defaults
-
watchPosition
Starts continuously watching the user's position, tied to the owner component's lifecycle.The browser reports new positions whenever it detects movement. Each report is delivered to the returned watcher's
valueSignal()signal on the UI thread. The initial value isGeolocationPendinguntil the first reading arrives, then transitions toGeolocationPosition(updated on every subsequent reading) orGeolocationError.The underlying browser watch is automatically cancelled when
ownerdetaches, so the application does not need to write cleanup code for navigation. For cancelling while the view is still attached (e.g. a "Stop watching" button), callGeolocationWatcher.stop()on the returned watcher.Permission-revoke caveat. If the user revokes geolocation permission while a watch is active and then grants it again, the browser silently stops delivering position updates to the existing watch — this is the W3C Geolocation API's documented behavior across browsers, not a Flow-specific limitation. To recover after a revoke/regrant cycle, call
GeolocationWatcher.stop()followed byGeolocationWatcher.resume(), which installs a fresh browser watch. Applications that want this to happen automatically can subscribe toavailabilityHintSignal()withSignal.effect(owner, ...)and trigger the stop/resume when the availability transitions back toGRANTED.- Parameters:
owner- the component that owns this watching session; detaching the component automatically stops the watch- Returns:
- a watcher whose
GeolocationWatcher.valueSignal()reports progress and whoseGeolocationWatcher.stop()cancels the watch
-
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 that owns this watching session; detaching the component automatically stops the watchoptions- accuracy / timeout / cache-age tuning, ornullto use the browser defaults- Returns:
- a watcher whose
GeolocationWatcher.valueSignal()reports progress and whoseGeolocationWatcher.stop()cancels the watch
-
availabilityHintSignal
Returns a read-only signal hinting at whether geolocation is usable for this 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
- Safari does not report permission state — every state surfaces as
-