1e41f4b71Sopenharmony_ci# Geofencing 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Scenario 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciA 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. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciCurrently, 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. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciA 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. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Available APIs 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciGeo-fencing uses the following interfaces. For details, see [Location Kit](../../reference/apis-location-kit/js-apis-geoLocationManager.md). 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci**Table 4** Geofencing APIs 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci| API| Description| 18e41f4b71Sopenharmony_ci| -------- | -------- | 19e41f4b71Sopenharmony_ci| [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.| 20e41f4b71Sopenharmony_ci| [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.| 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## How to Develop 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci1. Declare the **ohos.permission.APPROXIMATELY_LOCATION** permission. For details, see [Applying for Location Permissions](#location-permission-guidelines.md). 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci2. Import the **geoLocationManager**, **wantAgent**, and **BusinessError** modules. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci ```ts 29e41f4b71Sopenharmony_ci import { geoLocationManager } from '@kit.LocationKit'; 30e41f4b71Sopenharmony_ci import { wantAgent } from '@kit.AbilityKit'; 31e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit' 32e41f4b71Sopenharmony_ci ``` 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci3. Create a **WantAgentInfo** object. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci Scenario 1: Create a **WantAgentInfo** object for starting an ability. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci ```ts 39e41f4b71Sopenharmony_ci // Set the action type through operationType of WantAgentInfo. 40e41f4b71Sopenharmony_ci let wantAgentInfo:wantAgent.WantAgentInfo = { 41e41f4b71Sopenharmony_ci wants: [ 42e41f4b71Sopenharmony_ci { 43e41f4b71Sopenharmony_ci deviceId: '', 44e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 45e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 46e41f4b71Sopenharmony_ci action: '', 47e41f4b71Sopenharmony_ci entities: [], 48e41f4b71Sopenharmony_ci uri: '', 49e41f4b71Sopenharmony_ci parameters: {} 50e41f4b71Sopenharmony_ci } 51e41f4b71Sopenharmony_ci ], 52e41f4b71Sopenharmony_ci operationType: wantAgent.OperationType.START_ABILITY, 53e41f4b71Sopenharmony_ci requestCode: 0, 54e41f4b71Sopenharmony_ci wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG] 55e41f4b71Sopenharmony_ci }; 56e41f4b71Sopenharmony_ci ``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci Scenario 2: Create a **WantAgentInfo** object for releasing a public event. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci ```ts 61e41f4b71Sopenharmony_ci // Set the action type through operationType of WantAgentInfo. 62e41f4b71Sopenharmony_ci let wantAgentInfo:wantAgent.WantAgentInfo = { 63e41f4b71Sopenharmony_ci wants: [ 64e41f4b71Sopenharmony_ci { 65e41f4b71Sopenharmony_ci action: 'event_name', // Set the action name. 66e41f4b71Sopenharmony_ci parameters: {}, 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci ], 69e41f4b71Sopenharmony_ci operationType: wantAgent.OperationType.SEND_COMMON_EVENT, 70e41f4b71Sopenharmony_ci requestCode: 0, 71e41f4b71Sopenharmony_ci wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG], 72e41f4b71Sopenharmony_ci } 73e41f4b71Sopenharmony_ci ``` 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci4. Call **getWantAgent()** to create a **WantAgent** object. 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci 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. 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci ```ts 80e41f4b71Sopenharmony_ci let wantAgentObj : object | undefined = undefined; 81e41f4b71Sopenharmony_ci // Create a WantAgent object. 82e41f4b71Sopenharmony_ci wantAgent.getWantAgent(wantAgentInfo, (err, data) => { 83e41f4b71Sopenharmony_ci if (err) { 84e41f4b71Sopenharmony_ci console.error('getWantAgent err=' + JSON.stringify(err)); 85e41f4b71Sopenharmony_ci return; 86e41f4b71Sopenharmony_ci } 87e41f4b71Sopenharmony_ci console.info('getWantAgent success'); 88e41f4b71Sopenharmony_ci wantAgentObj = data; 89e41f4b71Sopenharmony_ci let requestInfo:geoLocationManager.GeofenceRequest = {'scenario': 0x301, "geofence": {"latitude": 31.12, "longitude": 121.11, "radius": 100, "expiration": 10000}}; 90e41f4b71Sopenharmony_ci try { 91e41f4b71Sopenharmony_ci geoLocationManager.on('gnssFenceStatusChange', requestInfo, wantAgentObj); 92e41f4b71Sopenharmony_ci } catch (err) { 93e41f4b71Sopenharmony_ci console.error("errCode:" + JSON.stringify(err)); 94e41f4b71Sopenharmony_ci } 95e41f4b71Sopenharmony_ci }); 96e41f4b71Sopenharmony_ci ``` 97