Class GeolocationApi
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:
getCurrentPosition(Element)- One-time position requestwatchPosition(Element)- Continuous position trackingclearWatch(String)- Stop watching position-
- Check if geolocation is available
invalid reference
#isSupported()
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 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(Element element) Requests the current geographic position using the current page context.static voidgetCurrentPosition(Element element, GeolocationOptions options) Requests the current geographic position with options using the current page context.static voidgetCurrentPosition(HtmlPage page, Element element) Requests the current geographic position.static voidgetCurrentPosition(HtmlPage page, Element element, GeolocationOptions options) Requests the current geographic position with options.static StringReturns JavaScript code to check if geolocation is supported.static StringwatchPosition(Element element) Starts watching the geographic position using the current page context.static StringwatchPosition(Element element, GeolocationOptions options) Starts watching the geographic position with options using the current page context.static StringwatchPosition(HtmlPage page, Element element) Starts watching the geographic position.static StringwatchPosition(HtmlPage page, Element element, GeolocationOptions options) Starts watching the geographic position with options.
-
Constructor Details
-
GeolocationApi
public GeolocationApi()
-
-
Method Details
-
getCurrentPosition
Requests the current geographic position using the current page context.This is a one-time request. When the position is obtained, a
GeolocationEventis dispatched to the element. If an error occurs, aGeolocationErrorEventis dispatched instead.- Parameters:
element- The element that will receive the position event.
-
getCurrentPosition
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
Requests the current geographic position.- Parameters:
page- The page context.element- The element that will receive the position event.
-
getCurrentPosition
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
Starts watching the geographic position using the current page context.This sets up continuous position tracking. A
GeolocationEventis dispatched to the element each time the position changes. The returned watch ID can be used to stop tracking viaclearWatch(String).- Parameters:
element- The element 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:
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
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
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
Stops watching the geographic position using the current page context.- Parameters:
watchId- The watch ID returned fromwatchPosition(Element).
-
clearWatch
Stops watching the geographic position.- Parameters:
page- The page context.watchId- The watch ID returned fromwatchPosition(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
method which will dispatch an event with the result.invalid reference
#checkSupported(HtmlPage, Element) -
checkSupported
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
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.
-