Class Geolocation

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

public class Geolocation extends Object implements Serializable
Facade for the browser's Geolocation API. Obtain via 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:

Availability check:
  • 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: