• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.enterprise.locationManager (Location Service Management)
2
3The **locationManager** module provides location service management capabilities for devices, including setting and obtaining the location service policy.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> The APIs of this module can be used only in the stage model.
10>
11> The APIs provided by this module can be called only by a [device administrator application](enterpriseDeviceManagement-overview.md#basic-concepts) that is [enabled](js-apis-enterprise-adminManager.md#adminmanagerenableadmin).
12
13## Modules to Import
14
15```ts
16import locationManager from '@ohos.enterprise.locationManager';
17```
18
19## locationManager.setLocationPolicy
20
21setLocationPolicy(admin: Want, policy: LocationPolicy): void
22
23Sets the location service policy. This API returns the result synchronously. If the operation fails, an exception will be thrown.
24
25**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_LOCATION
26
27**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
28
29**System API**: This is a system API.
30
31**Parameters**
32
33| Name  | Type                                 | Mandatory  | Description     |
34| ----- | ----------------------------------- | ---- | ------- |
35| admin | [Want](js-apis-app-ability-want.md) | Yes   | Device administrator application.|
36| policy | [LocationPolicy](js-apis-enterprise-locationManager.md) | Yes   | Location service policy to set. The value can be any of the following:<br>- **0**: default policy.<br>- **1**: The location service is disabled.<br>- **2**: The location service is forcibly enabled.|
37
38**Error codes**
39
40For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
41
42| ID| Error Message                                                                    |
43| ------- | ---------------------------------------------------------------------------- |
44| 9200001 | the application is not an administrator of the device.                        |
45| 9200002 | the administrator application does not have permission to manage the device. |
46
47**Example**
48
49```ts
50import Want from '@ohos.app.ability.Want';
51import locationManager from '@ohos.enterprise.locationManager';
52
53let wantTemp: Want = {
54  bundleName: 'com.example.myapplication',
55  abilityName: 'EntryAbility',
56};
57
58try {
59    locationManager.setLocationPolicy(wantTemp, locationManager.LocationPolicy.DISALLOW_LOCATION_SERVICE);
60    console.info(`Succeeded in setting location patch tag.`);
61} catch(err) {
62    console.error(`Failed to get location patch tag. Code: ${err.code}, message: ${err.message}`);
63}
64```
65## locationManager.getLocationPolicy
66
67getLocationPolicy(admin: Want): LocationPolicy
68
69Obtains the location service policy. This API returns the result synchronously. If the operation is successful, the location service policy is returned. If the operation fails, an exception will be thrown.
70
71**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_LOCATION
72
73**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
74
75**System API**: This is a system API.
76
77**Parameters**
78
79| Name     | Type                                      | Mandatory  | Description                      |
80| -------- | ---------------------------------------- | ---- | ------------------------------- |
81| admin    | [Want](js-apis-app-ability-want.md)     | Yes   | Device administrator application.                 |
82
83**Return value**
84
85| Type            | Description                                                  |
86| ---------------- | ------------------------------------------------------ |
87| LocationPolicy | Location service policy obtained.|
88
89**Error codes**
90
91For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
92
93| ID| Error Message                                                                      |
94| ------- | ---------------------------------------------------------------------------- |
95| 9200001 | the application is not an administrator of the device.                        |
96| 9200002 | the administrator application does not have permission to manage the device. |
97
98**Example**
99
100```ts
101import Want from '@ohos.app.ability.Want';
102import locationManager from '@ohos.enterprise.locationManager';
103let wantTemp: Want = {
104  bundleName: 'com.example.myapplication',
105  abilityName: 'EntryAbility',
106};
107
108try {
109    let result: locationManager.LocationPolicy = locationManager.getLocationPolicy(wantTemp);
110    console.info(`Succeeded in getting location policy. policy: ${result}`);
111} catch(err) {
112    console.error(`Failed to get device encryption status. Code: ${err.code}, message: ${err.message}`);
113}
114```
115
116## LocationPolicy
117
118Enumerates the location service policies.
119
120**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
121
122**System API**: This is a system API.
123
124**Model restriction**: This API can be used only in the stage model.
125
126| Name                       | Value | Description   |
127| ----------------------------| ----| ------------------------------- |
128| DEFAULT_LOCATION_SERVICE    | 0   | Default policy.|
129| DISALLOW_LOCATION_SERVICE | 1   | The location service is disabled.|
130| FORCE_OPEN_LOCATION_SERVICE | 2   | The location service is forcibly enabled.|
131