1# Geofencing 2 3## Scenario 4 5A geofence is a group of virtual bounds defining an area on the map. When a user device enters or leaves a geofence, or stays in a geofence, your app on the user device can automatically receive notifications and alarms. 6 7Currently, only circular fences are supported. In addition, the geo-fencing function of the GNSS chip is required. Events of entering or leaving the fence can be accurately identified only in open outdoor areas. 8 9A typical application of geofencing is to create a geofence around an enterprise for targeted advertising. In different areas, you can provide differentiated promotions for mobile devices. 10 11## Available APIs 12 13Geo-fencing uses the following interfaces. For details, see [Location Kit](../../reference/apis-location-kit/js-apis-geoLocationManager.md). 14 15**Table 4** Geofencing APIs 16 17| API| Description| 18| -------- | -------- | 19| [on(type: 'gnssFenceStatusChange', request: GeofenceRequest, want: WantAgent): void;](../../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanagerongnssfencestatuschange) | Registers a listener for status change events of the specified geofence.| 20| [off(type: 'gnssFenceStatusChange', request: GeofenceRequest, want: WantAgent): void;](../../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanageroffgnssfencestatuschange) | Unregisters the listener for status change events of the specified geofence.| 21 22## How to Develop 23 241. Declare the **ohos.permission.APPROXIMATELY_LOCATION** permission. For details, see [Applying for Location Permissions](#location-permission-guidelines.md). 25 262. Import the **geoLocationManager**, **wantAgent**, and **BusinessError** modules. 27 28 ```ts 29 import { geoLocationManager } from '@kit.LocationKit'; 30 import { wantAgent } from '@kit.AbilityKit'; 31 import { BusinessError } from '@kit.BasicServicesKit' 32 ``` 33 343. Create a **WantAgentInfo** object. 35 36 Scenario 1: Create a **WantAgentInfo** object for starting an ability. 37 38 ```ts 39 // Set the action type through operationType of WantAgentInfo. 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 Scenario 2: Create a **WantAgentInfo** object for releasing a public event. 59 60 ```ts 61 // Set the action type through operationType of WantAgentInfo. 62 let wantAgentInfo:wantAgent.WantAgentInfo = { 63 wants: [ 64 { 65 action: 'event_name', // Set the action 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. Call **getWantAgent()** to create a **WantAgent** object. 76 77 Call the geofencing API to add a geofence after obtaining the **WantAgent** object, and have the system automatically trigger the action defined for the **WantAgent** object when a device enters or exits the geofence. 78 79 ```ts 80 let wantAgentObj : object | undefined = undefined; 81 // Create a WantAgent object. 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