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), the registered GeolocationListener is notified.

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, MouseClickedEvent.class);
         body.addElement(locateBtn);
     }

     @Override
     public void onEvent(MouseClickedEvent event)
     {
         GeolocationApi.getCurrentPosition(this, GeolocationOptions.highAccuracy());
     }

     @Override
     public void onEvent(GeolocationEvent event)
     {
         locationDisplay.setText(String.format("Lat: %.6f, Lng: %.6f",
             event.getLatitude(), event.getLongitude()));
     }

     @Override
     public void onEvent(GeolocationErrorEvent event)
     {
         locationDisplay.setText("Error: " + event.getMessage());
     }
 }
 
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • GeolocationApi

      public GeolocationApi()
  • Method Details

    • getCurrentPosition

      public static void getCurrentPosition(GeolocationListener listener)
      Requests the current geographic position using the current page context.
      Parameters:
      listener - The listener that will receive the position or error event.
    • getCurrentPosition

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

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

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

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

      public static String watchPosition(GeolocationListener listener, GeolocationOptions options)
      Starts watching the geographic position with options using the current page context.
      Parameters:
      listener - The listener 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, GeolocationListener listener)
      Starts watching the geographic position.
      Parameters:
      page - The page context.
      listener - The listener that will receive position events.
      Returns:
      A watch ID that can be used to stop tracking.
    • watchPosition

      public static String watchPosition(HtmlPage page, GeolocationListener listener, GeolocationOptions options)
      Starts watching the geographic position with options.
      Parameters:
      page - The page context.
      listener - The listener 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(GeolocationListener).
    • 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, GeolocationListener).
    • checkSupported

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

      public static void checkSupported(HtmlPage page)
      Checks if geolocation is supported by the client browser.
      Parameters:
      page - The page context.
    • isSupportedJs

      public static String isSupportedJs()
      Returns JavaScript code to check if geolocation is supported.
      Returns:
      JavaScript expression that evaluates to true if geolocation is supported.