Class GeolocationApi

java.lang.Object
com.oorian.html.js.JavaScriptApi
com.oorian.html.js.geolocation.GeolocationApi

public class GeolocationApi extends JavaScriptApi
Provides access to the browser's Geolocation API from server-side Java code.

This class wraps the browser's navigator.geolocation API, enabling server-side code to request geographic position data from the client. When a position is obtained (or an error occurs), an event is dispatched to the element that initiated the request.

Methods:

Usage:


 public class LocationPage extends HtmlPage implements GeolocationListener {

     private Div locationDisplay;

     @Override
     protected void createBody(Body body) {
         locationDisplay = new Div();
         body.addElement(locationDisplay);

         Button locateBtn = new Button("Get My Location");
         locateBtn.registerListener(this, GeolocationEvent.class);
         locateBtn.registerListener(this, GeolocationErrorEvent.class);
         body.addElement(locateBtn);

         locateBtn.registerListener((MouseClickedEvent e) -> {
             GeolocationApi.getCurrentPosition(locateBtn, GeolocationOptions.highAccuracy());
         }, MouseClickedEvent.class);
     }

     @Override
     public void onEvent(GeolocationEvent event) {
         locationDisplay.setText(String.format(
             "Latitude: %.6f, Longitude: %.6f (accuracy: %.1fm)",
             event.getLatitude(),
             event.getLongitude(),
             event.getAccuracy()));
     }

     @Override
     public void onEvent(GeolocationErrorEvent event) {
         locationDisplay.setText("Error: " + event.getMessage());
     }
 }
 

Browser Support:

The Geolocation API is supported in all modern browsers. On mobile devices, location services must be enabled. For HTTPS pages, browsers will prompt the user for permission before sharing location data.

Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • GeolocationApi

      public GeolocationApi()
  • Method Details

    • getCurrentPosition

      public static void getCurrentPosition(Element element)
      Requests the current geographic position using the current page context.

      This is a one-time request. When the position is obtained, a GeolocationEvent is dispatched to the element. If an error occurs, a GeolocationErrorEvent is dispatched instead.

      Parameters:
      element - The element that will receive the position event.
    • getCurrentPosition

      public static void getCurrentPosition(Element element, GeolocationOptions options)
      Requests the current geographic position with options using the current page context.
      Parameters:
      element - The element that will receive the position event.
      options - Configuration options for the position request.
    • getCurrentPosition

      public static void getCurrentPosition(HtmlPage page, Element element)
      Requests the current geographic position.
      Parameters:
      page - The page context.
      element - The element that will receive the position event.
    • getCurrentPosition

      public static void getCurrentPosition(HtmlPage page, Element element, GeolocationOptions options)
      Requests the current geographic position with options.
      Parameters:
      page - The page context.
      element - The element that will receive the position event.
      options - Configuration options for the position request, or null for defaults.
    • watchPosition

      public static String watchPosition(Element element)
      Starts watching the geographic position using the current page context.

      This sets up continuous position tracking. A GeolocationEvent is dispatched to the element each time the position changes. The returned watch ID can be used to stop tracking via clearWatch(String).

      Parameters:
      element - The element that will receive position events.
      Returns:
      A watch ID that can be used to stop tracking.
    • watchPosition

      public static String watchPosition(Element element, GeolocationOptions options)
      Starts watching the geographic position with options using the current page context.
      Parameters:
      element - The element that will receive position events.
      options - Configuration options for position tracking.
      Returns:
      A watch ID that can be used to stop tracking.
    • watchPosition

      public static String watchPosition(HtmlPage page, Element element)
      Starts watching the geographic position.
      Parameters:
      page - The page context.
      element - The element that will receive position events.
      Returns:
      A watch ID that can be used to stop tracking.
    • watchPosition

      public static String watchPosition(HtmlPage page, Element element, GeolocationOptions options)
      Starts watching the geographic position with options.
      Parameters:
      page - The page context.
      element - The element that will receive position events.
      options - Configuration options for position tracking, or null for defaults.
      Returns:
      A watch ID that can be used to stop tracking.
    • clearWatch

      public static void clearWatch(String watchId)
      Stops watching the geographic position using the current page context.
      Parameters:
      watchId - The watch ID returned from watchPosition(Element).
    • clearWatch

      public static void clearWatch(HtmlPage page, String watchId)
      Stops watching the geographic position.
      Parameters:
      page - The page context.
      watchId - The watch ID returned from watchPosition(HtmlPage, Element).
    • checkSupported

      public static void checkSupported()
      Checks if geolocation is supported by the client browser using the current page context.

      This executes JavaScript to check for navigator.geolocation and returns immediately. The actual support check happens asynchronously on the client. For immediate results, use the static

      invalid reference
      #checkSupported(HtmlPage, Element)
      method which will dispatch an event with the result.

    • checkSupported

      public static void checkSupported(HtmlPage page)
      Checks if geolocation is supported by the client browser.

      Sends a script to check browser support. The result is logged to the console.

      Parameters:
      page - The page context.
    • isSupportedJs

      public static String isSupportedJs()
      Returns JavaScript code to check if geolocation is supported.

      This can be used in conditional JavaScript execution.

      Returns:
      JavaScript expression that evaluates to true if geolocation is supported.