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.

Find Geofencing React-Native library release notes in the CHANGELOG.md file on npm website.

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:

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

    

Adding the platform

For iOS

xml
        <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>fetch</string>
    <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'] = '13.0'
    end
  end
end

    

Supported Platforms

App closed limitations Because of the React-Native 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

Objects(Read Only)

Usage

JavaScript
        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.

JavaScript
        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.

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

    

Check Bluetooth permissions

In order to get beacon-fencing running on Android devices you need to make sure that all the Bluetooth related permissions are granted to the plugin.

To check if the Bluetooth permissions are granted by the user call getBLEPermissionsStatus method.

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

    

Parameter status will be a string, one of:

Requesting Bluetooth permissions

To request Bluetooth access call requestBLEPermissions method of the plugin. This will result in displaying Bluetooth access permission dialog.

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


    

Note: For beacon-fencing to work you will also need to request background location access. To request background location access please invoke requestPermissions method of the plugin with background parameter enabled.

Initializing the plugin

Plugin can be initialized by simply calling initialize method.

JavaScript
        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.

JavaScript
        await WoosmapGeofencing.initialize();

    

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

JavaScript
        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.

JavaScript
        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:

JavaScript
        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.

JavaScript
        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.

JavaScript
        WoosmapGeofencing.removeLocations()
        .then((value: string) => {
          console.log(value);
        })
        .catch((error: any) => {
          console.error(error);
        });

    
Was this article helpful?
Have more questions? Submit a request