Class GeolocationHelper


  • public class GeolocationHelper
    extends Object
    Static helper class that provides Java access to the browser's Geolocation API.

    This class wraps the native browser Geolocation API and provides static methods to request the user's current position. All operations are asynchronous and require user permission.

    Important: Geolocation operations require user permission and may be restricted in certain browsers or contexts (e.g., non-HTTPS).

    Example usage:

     // Request current position (results delivered via desktop events)
     GeolocationHelper.getCurrentPosition();
     

    Results are delivered asynchronously via GeolocationEvent posted to the desktop. Components can listen for these events to handle geolocation results.

    Based on MDN Geolocation API

    • Constructor Detail

      • GeolocationHelper

        public GeolocationHelper()
    • Method Detail

      • getCurrentPosition

        public static void getCurrentPosition()
        Request the current position from the browser. Results are delivered asynchronously via GeolocationEvent.

        Components can listen for geolocation events:

         @Listen(GeolocationEvent.EVENT_NAME + " = #root")
         public void handleGeolocation(GeolocationEvent event) {
             if (event.isSuccess()) {
                 GeoLocationPosition position = event.getGeoLocationPosition();
                 // Process position data
             } else {
                 GeolocationPositionError error = event.getGeoLocationPositionError();
                 // Handle error
             }
         }
         
        Throws:
        IllegalStateException - if called outside an execution context
      • init

        public static void init()
        Initialize geolocation helper for the current desktop if not already initialized. This method ensures the AU service and JavaScript are properly set up. You should call this method before you call getCurrentPosition(). You should call this method in a Composer's life cycle method e.g. Composer.doAfterCompose(Component).
      • addHelperScript

        protected static void addHelperScript​(org.zkoss.zk.ui.Desktop desktop)
      • addAuService

        protected static void addAuService​(org.zkoss.zk.ui.Desktop desktop)
      • dispose

        public static void dispose()
        Dispose geolocation helper for the current desktop. Removes the AU service listener and JavaScript helper for this desktop.
        Throws:
        IllegalStateException - if called outside an execution context