public class Locator extends Object
SystemCapability.Location#LOCATION| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
ERROR_PERMISSION_NOT_GRANTED |
Indicates that the location permission has not been granted when
the application calls the
Locator interface. |
static int |
ERROR_SWITCH_UNOPEN |
Indicates that the location service has been disabled when
the application calls the
Locator interface. |
static int |
LOCATION_SERVICE_SWITCH_OFF |
Indicates that the location service is disabled.
|
static int |
LOCATION_SERVICE_SWITCH_ON |
Indicates that the location service is enabled.
|
static int |
SESSION_START |
Indicates that the location request has been accepted by the system.
|
static int |
SESSION_STOP |
Indicates that the location request has finished.
|
| 构造器 | 说明 |
|---|---|
Locator(Context context) |
A constructor used to create a
Locator instance based on
the specified context to implement the location service. |
Locator(AbilityInfo info) |
已过时。
This change is effective since API 3, some app may not create locator object in ablitity,
Replaced by
Locator(Context context). |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
Location |
getCachedLocation() |
Obtains the location information cached in the system.
|
boolean |
isLocationSwitchOn() |
Checks whether the location service is enabled.
|
boolean |
registerSwitchCallback(SwitchCallback callback) |
Registers a callback object for obtaining the location service status in real time.
|
void |
requestEnableLocation() |
Requests the user to enable the location service.
|
void |
requestOnce(RequestParam request,
LocatorCallback callback) |
Sends a single location request to the system.
|
void |
startLocating(RequestParam request,
LocatorCallback callback) |
Sends location requests to the system.
|
void |
stopLocating(LocatorCallback callback) |
Ends the location service.
|
boolean |
unregisterSwitchCallback(SwitchCallback callback) |
Deregisters the callback object used for obtaining the location service status.
|
public static final int ERROR_PERMISSION_NOT_GRANTED
Locator interface.
The error is returned to the application through
LocatorCallback.onErrorReport(int).
public static final int ERROR_SWITCH_UNOPEN
Locator interface.
The error is returned to the application through
LocatorCallback.onErrorReport(int).
public static final int LOCATION_SERVICE_SWITCH_OFF
The status is returned to the application through
SwitchCallback.onSwitchChange(int).
public static final int LOCATION_SERVICE_SWITCH_ON
The status is returned to the application through
SwitchCallback.onSwitchChange(int).
public static final int SESSION_START
The status is returned to the application through
LocatorCallback.onStatusChanged(int).
public static final int SESSION_STOP
The status is returned to the application through
LocatorCallback.onStatusChanged(int).
@Deprecated public Locator(AbilityInfo info)
Locator(Context context).Locator instance based on
the specified ability information to implement the location service.info - info Indicates the ability information, which is specified by AbilityInfo.public boolean isLocationSwitchOn()
true if the location service is enabled; returns false otherwise.public void requestEnableLocation()
throws IllegalArgumentException
If the location service is already enabled, no operation is performed.
IllegalArgumentException - Throws this exception if Locator constructed without ability context.public void startLocating(RequestParam request, LocatorCallback callback) throws IllegalArgumentException
The system starts the location service based on the input location request parameters and reports the location result to the application through the callback passed in this method.
The system reports the location results to the application in real time until
the application proactively stops the location service through stopLocating(ohos.location.LocatorCallback).
To use this method, your application needs to apply for
the ohos.permission.LOCATION permission from the user.
If the location access permission has not been granted by the user when this method is called,
the system returns ERROR_PERMISSION_NOT_GRANTED to the application through LocatorCallback.
If the location service is disabled when this method is called,
the system returns error code ERROR_SWITCH_UNOPEN through LocatorCallback.
request - Indicates the location request parameters.callback - Indicates the callback for reporting the location result.IllegalArgumentException - Throws this exception if the specified RequestParam object or
the specified LocatorCallback object is null.RequestParam,
LocatorCallbackpublic void stopLocating(LocatorCallback callback) throws IllegalArgumentException
The specified callback object must be the same as that provided to the system when the location request is initiated.Otherwise, ending the location service will fail.
callback - Specifies the callback object for returning the location result.IllegalArgumentException - Throws this exception if the specified LocatorCallback object is null.LocatorCallbackpublic void requestOnce(RequestParam request, LocatorCallback callback) throws IllegalArgumentException
The system starts the location service based on the input location request parameters and reports the location result to the application through the callback passed in this method.
This location request is automatically stopped after the system reports the location result.
If the location cannot be completed within a reasonable amount of time due to environment issues,
you can call stopLocating(ohos.location.LocatorCallback) to enable the application to
proactively stop the location request.
To use this method, your application needs to apply for
the ohos.permission.LOCATION permission from the user.
If the location access permission has not been granted by the user when this method is called,
the system returns ERROR_PERMISSION_NOT_GRANTED to the application through LocatorCallback.
If the location service is disabled when this method is called,
the system returns error code ERROR_SWITCH_UNOPEN through LocatorCallback.
request - Indicates the location request parameters.callback - Indicates the callback for reporting the location result.IllegalArgumentException - Throws this exception if the specified RequestParam object or
the specified LocatorCallback object is null.RequestParam,
LocatorCallbackpublic Location getCachedLocation() throws SecurityException
You can call this method to obtain the cached location information if you do not need the real-time location data.
To use this method, you need to apply for the ohos.permission.LOCATION permission from the user.
null if no location information has been cached.SecurityException - Throws this exception
if your application is not granted the ohos.permission.LOCATION permission.Locationpublic boolean registerSwitchCallback(SwitchCallback callback) throws IllegalArgumentException
callback - Specifies the callback object for obtaining the location service status.
The value must not be null.true if the registration is successful; returns false otherwise.IllegalArgumentException - Throws this exception if the specified SwitchCallback object is null.SwitchCallbackpublic boolean unregisterSwitchCallback(SwitchCallback callback) throws IllegalArgumentException
callback - Specifies the callback object used for obtaining the location service status.
The value must not be null.true if the deregistration is successful; returns false otherwise.IllegalArgumentException - Throws this exception if the value of callback is null.SwitchCallback