1# 地理围栏开发指导(ArkTS) 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| 接口名 | 功能描述 | 16| -------- | -------- | 17| [addGnssGeofence(fenceRequest: GnssGeofenceRequest): Promise<number>](../../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanageraddgnssgeofence12) | 添加一个GNSS地理围栏,并订阅地理围栏事件。使用Promise异步回调。 | 18| [removeGnssGeofence(geofenceId: number): Promise<void>](../../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanagerremovegnssgeofence12) | 删除一个GNSS地理围栏,并取消订阅该地理围栏事件。使用Promise异步回调。 | 19 20## 开发步骤 21 221. 使用地理围栏功能,需要有权限ohos.permission.APPROXIMATELY_LOCATION,位置权限申请的方法和步骤见[申请位置权限开发指导](location-permission-guidelines.md)。 23 242. 导入geoLocationManager模块、wantAgent模块和BusinessError模块。 25 26 ```ts 27 import { geoLocationManager } from '@kit.LocationKit'; 28 import { BusinessError } from '@kit.BasicServicesKit'; 29 import { notificationManager } from '@kit.NotificationKit'; 30 ``` 31 323. 创建围栏。 33 34 ```ts 35 // 通过WantAgentInfo的operationType设置动作类型 36 let geofence: geoLocationManager.Geofence = { 37 "latitude": 34.12, "longitude": 124.11, "radius": 10000.0, "expiration": 10000.0 38 } 39 ``` 40 414. 指定APP需要监听的地理围栏事件类型,这里表示需要监听进入围栏和退出围栏事件。 42 43 ```ts 44 let transitionStatusList: Array<geoLocationManager.GeofenceTransitionEvent> = [ 45 geoLocationManager.GeofenceTransitionEvent.GEOFENCE_TRANSITION_EVENT_ENTER, 46 geoLocationManager.GeofenceTransitionEvent.GEOFENCE_TRANSITION_EVENT_EXIT, 47 ]; 48 ``` 49 504. 创建GEOFENCE_TRANSITION_EVENT_ENTER、GEOFENCE_TRANSITION_EVENT_EXIT事件对应的通知对象。 51 52 ```ts 53 // GEOFENCE_TRANSITION_EVENT_ENTER事件 54 let notificationRequest1: notificationManager.NotificationRequest = { 55 id: 1, 56 content: { 57 notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 58 normal: { 59 title: "围栏通知", 60 text: "围栏进入", 61 additionalText: "" 62 } 63 } 64 }; 65 // 创建GEOFENCE_TRANSITION_EVENT_EXIT事件对应的通知对象 66 let notificationRequest2: notificationManager.NotificationRequest = { 67 id: 2, 68 content: { 69 notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 70 normal: { 71 title: '围栏通知', 72 text: '围栏退出', 73 additionalText: "" 74 } 75 } 76 }; 77 ``` 78 795. 添加围栏。 80 81 ```ts 82 // 把创建的通知对象存入Array中,存入顺序与transitionStatusList一致 83 let notificationRequestList: Array<notificationManager.NotificationRequest> = 84 [notificationRequest1, notificationRequest2]; 85 // 构造GNSS地理围栏请求对象gnssGeofenceRequest 86 let gnssGeofenceRequest: geoLocationManager.GnssGeofenceRequest = { 87 // 围栏属性,包含圆心和半径等信息 88 geofence: geofence, 89 // 指定APP需要监听的地理围栏事件类型 90 monitorTransitionEvents: transitionStatusList, 91 // 地理围栏事件对应的通知对象,该参数为可选 92 notifications: notificationRequestList, 93 // 用于监听围栏事件的callback 94 geofenceTransitionCallback: (err : BusinessError, transition : geoLocationManager.GeofenceTransition) => { 95 if (err) { 96 console.error('geofenceTransitionCallback: err=' + JSON.stringify(err)); 97 } 98 if (transition) { 99 console.info("GeofenceTransition: %{public}s", JSON.stringify(transition)); 100 } 101 } 102 } 103 try { 104 // 添加围栏 105 geoLocationManager.addGnssGeofence(gnssGeofenceRequest).then((id) => { 106 // 围栏添加成功后返回围栏ID 107 console.info("addGnssGeofence success, fence id: " + id); 108 let fenceId = id; 109 }).catch((err: BusinessError) => { 110 console.error("addGnssGeofence failed, promise errCode:" + (err as BusinessError).code + 111 ",errMessage:" + (err as BusinessError).message); 112 }); 113 } catch(error) { 114 console.error("addGnssGeofence failed, err:" + JSON.stringify(error)); 115 } 116 ``` 117 1186. 删除围栏。 119 120 ```ts 121 // fenceId是在geoLocationManager.addGnssGeofence执行成功后获取的 122 let fenceId = 1; 123 try { 124 geoLocationManager.removeGnssGeofence(fenceId).then(() => { 125 console.info("removeGnssGeofence success fenceId:" + fenceId); 126 }).catch((error : BusinessError) => { 127 console.error("removeGnssGeofence: error=" + JSON.stringify(error)); 128 }); 129 } catch(error) { 130 console.error("removeGnssGeofence: error=" + JSON.stringify(error)); 131 } 132 ```