React Native Plugin

Setup the Woosmap Geofencing SDK using the React Native Plugin

  1. Install the React Native Plugin
  2. Usage

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

Install the React Native Plugin

Prerequisites

Before you begin, make sure you have installed and setup React Native on your machine. See this Get Started on official React Native Documentation.

Installation

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

npm install @woosmap/react-native-plugin-geofencing

Adding the platform

For iOS

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Used to test the library</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Used to test the library</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Used to test the library</string>
<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>
post_install do |installer|
installer.pods_project.targets.each do |target|
  target.build_configurations.each do |config|
        config.build_settings["ONLY_ACTIVE_ARCH"] = "NO"
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0'
    end
  end
end

Supported Platforms

Modules

Objects(Read Only)

Usage

import WoosmapGeofencing from '@woosmap/react-native-plugin-geofencing';

Check and request permissions

Before initializing the SDK it is required that you request for required location permissions.

To check if the location permissions are granted by the user call getPermissionsStatus method.

WoosmapGeofencing.getPermissionsStatus()
        .then((status: string) => {
          console.log(status);
        })
        .catch((error: any) => {
          alert('message: ' + error.message);
        });

Parameter status will be a string, one of:

Please note: Plugin will not work as expected if location access is denied.

Requesting location access To request location access call requestPermissions method of the plugin. This will result in displaying location access permission dialog. This method accepts a boolean parameter isBackground. If this parameter is set to true, then plugin will ask for background location access. Code snippet below asks for background location access.

WoosmapGeofencing.requestPermissions(props.background)
        .then((status: string) => {
          console.log(status);
        })
        .catch((error: any) => {
          alert('message: ' + error.message);
        });

Initializing the plugin

Plugin can be initialized by simply calling initialize method.

var woosmapSettings = {
  privateKeyWoosmapAPI: "<<WOOSMAP_KEY>>",
  trackingProfile: "liveTracking"
};
WoosmapGeofencing.initialize(woosmapSettings)
        .then((value: string) => {
          console.log(value);
        })
        .catch((error: any) => {
          alert('message: ' + error.message);
        });

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

await WoosmapGeofencing.initialize();

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

WoosmapGeofencing.setWoosmapApiKey(privateKeyWoosmapAPI)
        .then((value: string) => {
          console.log(value);
        })
        .catch((error: any) => {
          alert('message: ' + error.message);
        });

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.

const callback = (value: Location) => {
  alert('message: ' + JSON.stringify(value));
};

WoosmapGeofencing.watchLocation(callback)
        .then((watchRef: string) => {
          //Keep watchRef, it requires when you wish to remove location watch.
          console.log('Watch added');
        })
        .catch((error: any) => {
          alert('message: ' + error.message);
        });

To stop getting location updates:

WoosmapGeofencing.clearLocationWatch(watchID)
        .then((watchRef: string) => {
          console.log(watchRef);
        })
        .catch((error: any) => {
          alert('message: ' + error.message);
        });

Get locations from the local database

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

WoosmapGeofencing.getPois(locationId) //locationId is optional
        .then((value: Location[]) => {
          console.log(String(value.length));
        })
        .catch((error: any) => {
          console.error(error);
        });

Delete all locations from the local database

Call removeLocations method to remove all locations in the local db.

WoosmapGeofencing.removeLocations()
        .then((value: string) => {
          console.log(value);
        })
        .catch((error: any) => {
          console.error(error);
        });
Was this article helpful?
Have more questions? Submit a request