public class Geolocation
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
static class |
Geolocation.PositionOptions
Additional options for receiving the user's location.
|
Modifier | Constructor and Description |
---|---|
protected |
Geolocation()
Should be instantiated by
getIfSupported() . |
Modifier and Type | Method and Description |
---|---|
void |
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 |
getIfSupported()
Returns a
Geolocation if the browser supports this feature, and
null otherwise. |
static boolean |
isSupported()
Returns
true 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.
|
protected Geolocation()
getIfSupported()
.public static Geolocation getIfSupported()
Geolocation
if the browser supports this feature, and
null
otherwise.public static boolean isSupported()
true
if the browser supports geolocation.public void clearWatch(int watchId)
watchId
- the ID of a position watch as returned by a previous call to
watchPosition(Callback)
.public void getCurrentPosition(Callback<Position,PositionError> callback)
public void getCurrentPosition(Callback<Position,PositionError> callback, Geolocation.PositionOptions options)
public int watchPosition(Callback<Position,PositionError> callback)
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.
clearWatch(int)
to stop watching the user's position.public int watchPosition(Callback<Position,PositionError> callback, Geolocation.PositionOptions 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.
clearWatch(int)
to stop watching the user's position.