Package org.zkoss.zkforge.geolocation
Class GeolocationHelper
- java.lang.Object
-
- org.zkoss.zkforge.geolocation.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
GeolocationEventposted to the desktop. Components can listen for these events to handle geolocation results.Based on MDN Geolocation API
-
-
Field Summary
Fields Modifier and Type Field Description protected static GeoLocationAuServiceauServiceprotected static StringGEOLOCATION_HELPER_JS_PATHprotected static StringGEOLOCATION_HELPER_KEY
-
Constructor Summary
Constructors Constructor Description GeolocationHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static voidaddAuService(org.zkoss.zk.ui.Desktop desktop)protected static voidaddHelperScript(org.zkoss.zk.ui.Desktop desktop)static voiddispose()Dispose geolocation helper for the current desktop.static voidgetCurrentPosition()Request the current position from the browser.static voidinit()Initialize geolocation helper for the current desktop if not already initialized.
-
-
-
Field Detail
-
GEOLOCATION_HELPER_KEY
protected static final String GEOLOCATION_HELPER_KEY
- See Also:
- Constant Field Values
-
GEOLOCATION_HELPER_JS_PATH
protected static final String GEOLOCATION_HELPER_JS_PATH
- See Also:
- Constant Field Values
-
auService
protected static GeoLocationAuService auService
-
-
Method Detail
-
getCurrentPosition
public static void getCurrentPosition()
Request the current position from the browser. Results are delivered asynchronously viaGeolocationEvent.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 callgetCurrentPosition(). 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
-
-