# Examples

### Install the module

Run the following command to install the module (make sure to install the **core** module beforehand):

```bash
npm i @sentiance-react-native/smart-geofences
```

### Import the module

```javascript
import SentianceSmartGeofences from "@sentiance-react-native/smart-geofences";
```

You can find a reference to all the types mentioned on this page [here](https://github.com/sentiance/react-native-sentiance/blob/main/packages/smart-geofences/lib/index.d.ts).

#### Refresh the list of monitored geofences

<pre class="language-javascript"><code class="lang-javascript">import {refreshGeofences} from "@sentiance-react-native/smart-geofences";

<strong>try {
</strong>     await refreshGeofences();
     console.log("Geofences refreshed successfully.");
} catch (error) {
     const refreshError = error.userInfo;
     const {reason, details} = refreshError;
     
     console.error("Refresh error reason:", reason);
     console.error("Refresh error details:", details);
}
</code></pre>

#### Listen to smart geofence entry/exit events

<pre class="language-javascript"><code class="lang-javascript"><strong>import {addSmartGeofenceEventListener} from "@sentiance-react-native/smart-geofences";
</strong>
const subscription = addSmartGeofenceEventListener(smartGeofenceEvent => {
  // Smart geofence entry/exit detected
  
  const eventTime = smartGeofenceEvent.timestamp;
  const triggeringLocation = smartGeofenceEvent.triggeringLocation;
  const eventType = smartGeofenceEvent.eventType;
  
  const geofences = smartGeofenceEvent.geofences;
  geofences.forEach(geofence => {
    const sentianceId = geofence.sentianceId;
    const latitude = geofence.latitude;
    const longitude = geofence.longitude;
    const radius = geofence.radius;
    const externalId = geofence.externalId;
  });
});

// Don't forget to unsubscribe, typically in componentWillUnmount
subscription.remove();
</code></pre>

#### Get the smart geofences detection mode

```javascript
import {getDetectionMode} from "@sentiance-react-native/smart-geofences";

const detectionMode = await getDetectionMode();
console.log('Detection mode is currently:', detectionMode);
```
