1# 基于设备自身定位的地理围栏开发指导
2
3## 场景概述
4
5地理围栏就是虚拟地理边界,当设备进入、离开某个特定地理区域时,可以接收自动通知和警告。
6
7目前仅支持圆形围栏,并且依赖GNSS芯片的地理围栏功能,仅在室外开阔区域才能准确识别用户进出围栏事件。
8
9应用场景举例:开发者可以使用地理围栏,在企业周围创建一个区域进行广告定位,在不同的地点,在移动设备上进行有针对性的促销优惠。
10
11## 接口说明
12
13地理围栏所使用的接口如下,详细说明参见:[Location Kit](../../reference/apis-location-kit/js-apis-geoLocationManager.md)。
14
15**表4** 地理围栏接口介绍
16
17| 接口名 | 功能描述 |
18| -------- | -------- |
19| [on(type: 'gnssFenceStatusChange', request: GeofenceRequest, want: WantAgent): void;](../../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanagerongnssfencestatuschange) | 添加一个围栏,并订阅地理围栏事件。 |
20| [off(type: 'gnssFenceStatusChange', request: GeofenceRequest, want: WantAgent): void;](../../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanageroffgnssfencestatuschange) | 删除一个围栏,并取消订阅该围栏事件。 |
21
22## 开发步骤
23
241. 使用地理围栏功能,需要有权限ohos.permission.APPROXIMATELY_LOCATION,位置权限申请的方法和步骤见[申请位置权限开发指导](location-permission-guidelines.md)。
25
262. 导入geoLocationManager模块、wantAgent模块和BusinessError模块。
27
28   ```ts
29   import { geoLocationManager } from '@kit.LocationKit';
30   import { wantAgent } from '@kit.AbilityKit';
31   import { BusinessError } from '@kit.BasicServicesKit'
32   ```
33
343. 创建WantAgentInfo信息。
35
36   场景一:创建拉起Ability的WantAgentInfo信息。
37
38   ```ts
39   // 通过WantAgentInfo的operationType设置动作类型
40   let wantAgentInfo:wantAgent.WantAgentInfo = {
41       wants: [
42           {
43               deviceId: '',
44               bundleName: 'com.example.myapplication',
45               abilityName: 'EntryAbility',
46               action: '',
47               entities: [],
48               uri: '',
49               parameters: {}
50           }
51       ],
52       operationType: wantAgent.OperationType.START_ABILITY,
53       requestCode: 0,
54       wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
55   };
56   ```
57
58   场景二:创建发布公共事件的WantAgentInfo信息。
59
60   ```ts
61   // 通过WantAgentInfo的operationType设置动作类型
62   let wantAgentInfo:wantAgent.WantAgentInfo = {
63       wants: [
64           {
65               action: 'event_name', // 设置事件名
66               parameters: {},
67           }
68       ],
69       operationType: wantAgent.OperationType.SEND_COMMON_EVENT,
70       requestCode: 0,
71       wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG],
72   }
73   ```
74
754. 调用getWantAgent()方法进行创建WantAgent。
76
77   并且在获取到WantAgent对象之后调用地理围栏接口添加围栏,当设备进入或者退出该围栏时,系统会自动触发WantAgent的动作。
78
79   ```ts
80   let wantAgentObj : object | undefined = undefined;
81   // 创建WantAgent
82   wantAgent.getWantAgent(wantAgentInfo, (err, data) => {
83       if (err) {
84         console.error('getWantAgent err=' + JSON.stringify(err));
85         return;
86       }
87       console.info('getWantAgent success');
88       wantAgentObj = data;
89       let requestInfo:geoLocationManager.GeofenceRequest = {'scenario': 0x301, "geofence": {"latitude": 31.12, "longitude": 121.11, "radius": 100, "expiration": 10000}};
90       try {
91           geoLocationManager.on('gnssFenceStatusChange', requestInfo, wantAgentObj);
92       } catch (err) {
93           console.error("errCode:" + JSON.stringify(err));
94       }
95   });
96   ```
97