Source: https://developers.woosmap.com/products/geofencing-sdk/cordova-plugin/guides/setup/

> For clean Markdown of any page, append `.md` to the page URL.

> For a complete documentation index, see https://developers.woosmap.com/llms.txt

# Cordova Plugin



The Woosmap Geofencing SDK allows you to monitor Geofences, track your user's location and connect with the Woosmap Search and Distance APIs.

Learn how to integrate the Cordova Plugin below. You can also explore [the source code on GitHub](https://github.com/woosmap/woosmap-geofencing-cordova-plugin) to get deeper into it.

## Install the Cordova Plugin

### Prerequisites

Before you begin, make sure you have installed and setup Cordova on your machine.
See this [Get Started on official Cordova Documentation](https://cordova.apache.org/docs/en/10.x/guide/cli/index.html).

**Create your cordova app**

```
cordova create <folder_name> <package_name> <project_name>
cd <folder_name>
```

**Adding the platform**

For iOS

```
cordova platform add ios@6.0.0
cordova plugin add cordova-plugin-add-swift-support --save
```

For Android

```
cordova plugin add cordova-plugin-androidx
cordova platform add android
```

### Woosmap Geofencing Cordova plugin

Add the Woosmap Geofencing Cordova plugin to your project using this command line:

```
cordova plugin add @woosmap/cordova-plugin-geofencing --link
```

## Running the plugin on the Android platform

Once you have done this build and run the project using following command:

```
cordova build
cordova run <android>/<ios>
```

### Supported Platforms

- iOS (Plugin supports iOS 12 and higher)
- Android (Plugin supports Android 9 and higher)

**App closed limitations**
Because of the Cordova framework, callbacks are not triggered when the application is closed.
Locations and geofence events are well stored in the device, and you can retrieve them through getter methods included in this plugin.

### Modules

- **Woosmap**: Woosmap contains methods to monitor location, POIs, regions and visits.
- **WoosmapDb**: contains methods to fetch POIs, regions and visits from the local device DB.

### Objects(Read Only)

- **Location**: Represents the location object
- **POI**: Represents Point of Interest
- **Region**: Represents a geographical region/Geofence
- **Visit**: Represents a visit to a location/POI
- **ZOI**: Represents Zone of Interest
- **Airship**: Contains custom data related to Airship implementation
- **MarketingCloud**: Contains custom data related to third party marketing cloud implementation

### Initializing the plugin

The plugin automatically sends the required authentication headers (`X-Api-Key` and platform identifiers) with every API request. If you're calling Woosmap REST APIs directly without the plugin, you'll need to set these headers yourself. See [Authenticating from Mobile Apps](/api-reference/authentication/#authenticating-from-mobile-apps-or-server-side-via-headers) for details.

Plugin can be initialized by simply calling `initialize` method.

```js
var woosmapSettings = {
  privateKeyWoosmapAPI: "<<WOOSMAP_KEY>>",
  trackingProfile: "liveTracking",
};
cordova.plugins.Woosmap.initialize(woosmapSettings, onSuccess, onError);
var onSuccess = function () {
  console.log("success");
};
var onError = function (error) {
  alert("message: " + error.message);
};
```

Both configuration options `privateKeyWoosmapAPI` and `trackingProfile` are optional. You can also initialize the plugin
by passing null configuration.

```js
cordova.plugins.Woosmap.initialize(null, onSuccess, onError);
```

You can also set the Woosmap API key later by calling `setWoosmapApiKey` method.

```js
cordova.plugins.Woosmap.setWoosmapApiKey(<privateKeyWoosmapAPI>, onSuccess, onError);
```

### Listening to location event

To listen to location, call `watchLocation` method. Method will invoke callback and pass a location object as a
parameter. Method will return a watchId . This id can be used to remove a callback.

```js
const locationCallback = function (location) {
  const wgsLocation = window.WoosmapGeofencing.Location.jsonToObj(location);
  console.log("Location:" + wgsLocation.Latitude + "," + wgsLocation.Longitude);
};

const error = function (err) {
  console.log(err);
};

const locationWatchId = cordova.plugins.WoosmapGeofencing.watchLocation(locationCallback, error);
```

To stop getting location updates:

```js
cordova.plugins.WoosmapGeofencing.clearLocationWatch(locationWatchId, success, error);
```

### Get locations from the local database

Call `getLocations` method to get an array of Locations from the local db.

```js
const success = function (locations) {
  console.log("Locations >>", locations);
  showSuccessToast("Total Locations: " + locations.length);
};
cordova.plugins.WoosmapDb.getLocations(success, error);
```
