React Native Plugin
Setup the Woosmap Geofencing SDK using the React Native Plugin
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:
npm install @woosmap/react-native-plugin-geofencing
Adding the platform
For iOS
- info.plist: Please check info.plist updated with following keys:
- NSLocationAlwaysAndWhenInUseUsageDescription
- NSLocationAlwaysUsageDescription
- NSLocationWhenInUseUsageDescription
- UIBackgroundModes
<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>
- Podfile: configure to use
use_frameworks!
andplatform :ios, '13.0'
if you are using Mac M1 architecture, update pod post installation as:
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
- iOS (Plugin supports iOS 13 and higher)
- Android (Plugin supports Android 9 and higher)
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
- WoosmapGeofencing: Woosmap contains methods to monitor location, regions.
Objects(Read Only)
- Location: Represents the location object
- POI: Represents Point of Interest
- Region: Represents a geographical region/Geofence
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:
GRANTED_BACKGROUND
: User has granted location access even when app is not running in the foreground.GRANTED_FOREGROUND
: Location access is granted only while user is using the app.DENIED
: Location access is denied.UNKNOWN
: Without providing or denying any permission then it will return unknown.
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);
});
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.
WoosmapGeofencing.getBLEPermissionsStatus()
.then((status: string) => {
console.log(status);
})
.catch((error: any) => {
alert('message: ' + error.message);
});
Parameter status will be a string, one of:
GRANTED
: User has granted bluetooth access.DENIED
: Bluetooth access is denied.BACKGROUND_LOCATION_DENIED
: Background location access is denied. You first need to request background location permission.
Requesting Bluetooth permissions
To request Bluetooth access call requestBLEPermissions
method of the plugin. This will result in displaying Bluetooth access permission dialog.
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.
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);
});