Class Geolocation

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

public final class Geolocation extends Object implements Serializable
Browser geolocation API for Flow applications. Two entry points: Every call is asynchronous and returns immediately; the browser answers later and Flow invokes the supplied consumers on the UI thread. The first call shows the browser's permission dialog if no decision has been recorded yet; if the user denies, the error consumer receives a 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: