Class Geolocation

java.lang.Object
org.vaadin.firitin.geolocation.Geolocation

public class Geolocation extends Object
A helper class to detect the geographical position of the end users.

This class uses the Geolocation API in the browser to detect the position of the user. The API mimics the JS counterpart.

Note that the availability and quality of the position data can vary a lot. Users can decline the geolocation request in the browser altogether, but developers can also affect the settings using GeolocationOptions.

Author:
mstahv
  • Constructor Details

    • Geolocation

      public Geolocation()
  • Method Details

    • watchPosition

      public static Geolocation watchPosition(Geolocation.UpdateListener listener, Geolocation.ErrorListener errorListener)
      Starts to repeatedly watch the geolocation of the device and notifies listener with the data.
      Parameters:
      listener - the listener called on succesful geolocation request
      errorListener - the listener called in case the request failed (e.g. user declined the request in the browser)
      Returns:
      a Geolocation instance that can be used to cancel requesting the data
    • watchPosition

      public static Geolocation watchPosition(Geolocation.UpdateListener listener, Geolocation.ErrorListener errorListener, GeolocationOptions options)
      Starts to repeatedly watch the geolocation of the device and notifies listener with the data.
      Parameters:
      listener - the listener called on succesful geolocation request
      errorListener - the listener called in case the request failed (e.g. user declined the request in the browser)
      options - options for the geolocation request
      Returns:
      a Geolocation instance that can be used to cancel requesting the data
    • watchPosition

      public static Geolocation watchPosition(com.vaadin.flow.component.UI ui, Geolocation.UpdateListener listener, Geolocation.ErrorListener errorListener, GeolocationOptions options)
      Starts to repeatedly watch the geolocation of the device and notifies listener with the data.
      Parameters:
      ui - the UI in which context the geolocation request is to be executed
      listener - the listener called on succesful geolocation request
      errorListener - the listener called in case the request failed (e.g. user declined the request in the browser)
      options - options for the geolocation request
      Returns:
      a Geolocation instance that can be used to cancel requesting the data
    • getCurrentPosition

      public static void getCurrentPosition(Geolocation.UpdateListener listener, Geolocation.ErrorListener errorListener, GeolocationOptions options)
      Determines the device's current location once and notifies listener with the data.
      Parameters:
      listener - the listener called on succesful geolocation request
      errorListener - the listener called in case the request failed (e.g. user declined the request in the browser)
      options - options for the geolocation request
    • getCurrentPosition

      public static void getCurrentPosition(com.vaadin.flow.component.UI ui, Geolocation.UpdateListener listener, Geolocation.ErrorListener errorListener, GeolocationOptions options)
      Determines the device's current location once and notifies listener with the data.
      Parameters:
      ui - the UI in which context the geolocation request is to be executed
      listener - the listener called on succesful geolocation request
      errorListener - the listener called in case the request failed (e.g. user declined the request in the browser)
      options - options for the geolocation request
    • getCurrentPosition

      public static void getCurrentPosition(Geolocation.UpdateListener listener, Geolocation.ErrorListener errorListener)
      Determines the device's current location once and notifies listener with the data.
      Parameters:
      listener - the listener called on succesful geolocation request
      errorListener - the listener called in case the request failed (e.g. user declined the request in the browser)
    • cancel

      public void cancel()
      Stops polling the listeners with the new geolocation data.