Class Geolocation
You can obtain a user's position by first calling
Geolocation.getIfSupported()
Once you have a Geolocation
, you can request the user's current
position by calling getCurrentPosition(Callback)
or
watchPosition(Callback)
.
The first time an application requests the user's position, the browser will
prompt the user for permission. If the user grants permission, the browser
will locate the user and report it back to your application. If the user
declines permission, the callback's Callback.onFailure(Object)
method
will be called with a PositionError
with its code set to
PositionError.PERMISSION_DENIED
.
Experimental API: This API is still under development and is subject to change.
This may not be supported on all browsers.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Additional options for receiving the user's location. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clearWatch
(int watchId) Stops watching the user's position.void
getCurrentPosition
(Callback<Position, PositionError> callback) Calls the callback with the user's current position.void
getCurrentPosition
(Callback<Position, PositionError> callback, Geolocation.PositionOptions options) Calls the callback with the user's current position, with additional options.static Geolocation
Returns aGeolocation
if the browser supports this feature, andnull
otherwise.static boolean
Returnstrue
if the browser supports geolocation.int
watchPosition
(Callback<Position, PositionError> callback) Repeatedly calls the given callback with the user's position, as it changes.int
watchPosition
(Callback<Position, PositionError> callback, Geolocation.PositionOptions options) Repeatedly calls the given callback with the user's position, as it changes, with additional options.
-
Constructor Details
-
Geolocation
protected Geolocation()Should be instantiated bygetIfSupported()
.
-
-
Method Details
-
getIfSupported
Returns aGeolocation
if the browser supports this feature, andnull
otherwise. -
isSupported
public static boolean isSupported()Returnstrue
if the browser supports geolocation. -
clearWatch
public void clearWatch(int watchId) Stops watching the user's position.- Parameters:
watchId
- the ID of a position watch as returned by a previous call towatchPosition(Callback)
.
-
getCurrentPosition
Calls the callback with the user's current position. -
getCurrentPosition
public void getCurrentPosition(Callback<Position, PositionError> callback, Geolocation.PositionOptions options) Calls the callback with the user's current position, with additional options. -
watchPosition
Repeatedly calls the given callback with the user's position, as it changes.The frequency of these updates is entirely up to the browser. There is no guarantee that updates will be received at any set interval, but are instead designed to be sent when the user's position changes. This method should be used instead of polling the user's current position.
- Returns:
- the ID of this watch, which can be passed to
clearWatch(int)
to stop watching the user's position.
-
watchPosition
public int watchPosition(Callback<Position, PositionError> callback, Geolocation.PositionOptions options) Repeatedly calls the given callback with the user's position, as it changes, with additional options.The frequency of these updates is entirely up to the browser. There is no guarantee that updates will be received at any set interval, but are instead designed to be sent when the user's position changes. This method should be used instead of polling the user's current position.
If the browser does not support geolocation, this method will do nothing, and will return -1.
- Returns:
- the ID of this watch, which can be passed to
clearWatch(int)
to stop watching the user's position.
-