Geolocation
The Geolocation class can be used to register events that get the geographical position of the device with a timestamp.
Instruction:
-
Creating the geolocation object. This must attach to our website in order to listen to events:
// We're currently located in the Constructor of a view final Geolocation geolocation = new Geolocation(this);
-
After that we can register our listeners:
// Outputs the Latitude und Longitude within the Consoles geolocation.addPositionListener(position -> { final Coordinates coordinates = position.getCoords(); System.out.println(String.format("Coordinates: lat=%f, long=%f", coordinates.getLatitude(), coordinates.getLongitude())); }); // Print the error message, if something doesn't work geolocation.addPositionErrorConsumer(error -> { System.err.println(error.getMessage()); });
-
If the position is to be output only once, getCurrentPosition(PositionOptions) can be called:
geolocation.getCurrentPosition(PositionOptions.Default());
-
To start listening to the position, watchPosition(PositionOptions) can be called:
geolocation.watchPosition(PositionOptions.Default());
Note:
-
Latitude, Longitude and Accuracy are always set.
-
Altitude, Accuracy, Heading and Speed do not have to be set by the browser, so these values can also contain zero!