Package com.oorian.html.js.geolocation
Class GeolocationApi
java.lang.Object
com.oorian.html.js.JavaScriptApi
com.oorian.html.js.geolocation.GeolocationApi
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidChecks if geolocation is supported by the client browser using the current page context.static voidcheckSupported(HtmlPage page) Checks if geolocation is supported by the client browser.static voidclearWatch(HtmlPage page, String watchId) Stops watching the geographic position.static voidclearWatch(String watchId) Stops watching the geographic position using the current page context.static voidgetCurrentPosition(HtmlPage page, GeolocationListener listener) Requests the current geographic position.static voidgetCurrentPosition(HtmlPage page, GeolocationListener listener, GeolocationOptions options) Requests the current geographic position with options.static voidgetCurrentPosition(GeolocationListener listener) Requests the current geographic position using the current page context.static voidgetCurrentPosition(GeolocationListener listener, GeolocationOptions options) Requests the current geographic position with options using the current page context.static StringReturns JavaScript code to check if geolocation is supported.static StringwatchPosition(HtmlPage page, GeolocationListener listener) Starts watching the geographic position.static StringwatchPosition(HtmlPage page, GeolocationListener listener, GeolocationOptions options) Starts watching the geographic position with options.static StringwatchPosition(GeolocationListener listener) Starts watching the geographic position using the current page context.static StringwatchPosition(GeolocationListener listener, GeolocationOptions options) Starts watching the geographic position with options using the current page context.
-
Constructor Details
-
GeolocationApi
public GeolocationApi()
-
-
Method Details
-
getCurrentPosition
Requests the current geographic position using the current page context.- Parameters:
listener- The listener that will receive the position or error event.
-
getCurrentPosition
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
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
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
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
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
Stops watching the geographic position using the current page context.- Parameters:
watchId- The watch ID returned fromwatchPosition(GeolocationListener).
-
clearWatch
Stops watching the geographic position.- Parameters:
page- The page context.watchId- The watch ID returned fromwatchPosition(HtmlPage, GeolocationListener).
-
checkSupported
public static void checkSupported()Checks if geolocation is supported by the client browser using the current page context. -
checkSupported
Checks if geolocation is supported by the client browser.- Parameters:
page- The page context.
-
isSupportedJs
Returns JavaScript code to check if geolocation is supported.- Returns:
- JavaScript expression that evaluates to true if geolocation is supported.
-