Skip to main content

GeofenceMgr (React Native)

Contents

A native module used for managing geofences. A geofence defines a geographic area that will either generate an alert when a route enters it, or will be avoided by the route.

A geofence has many different applications, including:

  • Drawing zones around a warehouse to identify delivery or billing zones.
  • Alerting dispatchers when a vehicle enters an area that it wasn’t authorized to enter – for example, leaving the country.
  • Avoiding or warning about steep grades, mountain passes susceptible to snow closures, tunnels requiring escorts, etc.
  • Avoiding or warning if an asset has crossed into a high crime or high accident area.
React Native
>=10.26
Type
Native Module

Methods

Method Name Return Type Description
addGeofencesXML(String)
void
Adds geofences from an XML file.
addGeofenceSet(GeofenceSet)
void
Adds a geofence set. If a set with the same name exists, it will be overwritten.
addGeofences(String, List)
void
Adds a list of geofences to an existing geofence set. If the set does not exist, or if the set name is blank, the fences will be added to the default set.
getGeofenceSet(String)
GeofenceSet
Gets a geofence set by its name.
getGeofence(String, String)
Geofence
Gets an individual geofence using its name and set name.
getGeofences()
List<GeofenceSet JSON Object>
Gets a list of all of CoPilot’s current geofences.
deleteGeofences(String, List)
void
Deletes geofences, geofence sets, or both. If both parameters are null, all geofences and geofence sets will be deleted.

Hooks and Callbacks

Hooks and Callbacks related to Geofencing can be found below.

GeofenceListener

  • onGeofenceEntry
  • onGeofenceExit
  • onGeofenceRouteThrough

GeofenceMgr.addGeofencesXML

Overview
Description Imports geofences from an XML document.
Supported on React Native Since Version 10.26
Type Method

Syntax addGeofencesXML(string xmlDocument)

Parameters

string xmlDocument - The contents of the xml file to import

Return Value

None

Sample Code

//Read the contents of the XML file the full file path of geofenceXmlFile
var xmlDocument = "...";
GeofenceMgr.addGeofencesXML(xmlDocument);

XML Format

Single Polygon Set:
<ArrayOfMapObjects Name="SampleSet" Persistent="1" NotifySDKOnEnterExit="both" NotifyUIWhileInside="1" NotifySDKOnRouteThrough="1"   ActiveState="off" BeepOnEnterExit="0" InnerColor="0xb0345678" OuterColor="0x00ff0000"  DrawBelowRoads="0" >
  <MapObject Type="Polygon" Label="Philly Area"  Name="PolygonA">
    <Point lat="40.000000" lon="-75.100000">
    <Point lat="40.000000" lon="-75.300000">
    <Point lat="39.870000" lon="-75.300000">
    <Point lat="39.870000" lon="-75.100000">
  </MapObject>
<MapObject Type="Polygon" Label="Cape May Area"  Name="PolygonB">
    <Point lat="38.930000" lon="-74.980000">
    <Point lat="38.980000" lon="-74.970000">
    <Point lat="38.950000" lon="-74.870000">
    <Point lat="38.940000" lon="-74.900000">
    <Point lat="38.920000" lon="-74.926000">
  </MapObject>
</ArrayOfMapObjects>

Multiple Polygon Sets:
<MapObjectSets>
<ArrayOfMapObjects Name="SampleSet1" Persistent="1" NotifySDKOnEnterExit="both" NotifyUIWhileInside="1" NotifySDKOnRouteThrough="1"   ActiveState="off" BeepOnEnterExit="0"  InnerColor="0xb0345678"  OuterColor="0x00ff0000"  DrawBelowRoads="0">
  <MapObject Type="Polygon" Label="Philly Area" Name="PolygonA">
    <Point lat="40.000000" lon="-75.100000">
    <Point lat="40.000000" lon="-75.300000">
    <Point lat="39.870000" lon="-75.300000">
    <Point lat="39.870000" lon="-75.100000">
  </MapObject>
<MapObject Type="Polygon"   Label="Cape May Area"  Name="PolygonB">
    <Point lat="38.930000" lon="-74.980000">
    <Point lat="38.980000" lon="-74.970000">
    <Point lat="38.950000" lon="-74.870000">
    <Point lat="38.940000" lon="-74.900000">
    <Point lat="38.920000" lon="-74.926000">
  </MapObject>
</ArrayOfMapObjects>
<ArrayOfMapObjects Name="SampleSet2" Persistent="1" NotifySDKOnEnterExit="none" NotifyUIWhileInside="1" NotifySDKOnRouteThrough="1"   ActiveState="avoid" BeepOnEnterExit="1"  InnerColor="0xb0345678"  OuterColor="0x00ff0000"  DrawBelowRoads="0">
  <MapObject Type="Polygon" Label="Jersey City  Area"  Name="PolygonC">
    <Point lat="40.711400" lon="-74.064800">
    <Point lat="40.711400" lon="-74.064800">
    <Point lat="40.801400" lon="-74.864800">
    <Point lat="40.951400" lon="-74.964800">
  </MapObject>
  <MapObject Type="Polygon"  Label="Hoboken Area" Name="PolygonD">
    <Point lat="43.745400" lon="-74.027900">
    <Point lat="43.745400" lon="-74.027900">
    <Point lat="43.775400" lon="-74.727900">
    <Point lat="43.785400" lon="-74.827900">
    <Point lat="43.805400" lon="-74.927900">
  </MapObject>
</ArrayOfMapObjects>
</MapObjectSets>

ArrayOfMapObjects properties

Property Description Required/Optional
Name The name of the polygon set. Optional
Persistent Whether or not this set should be saved and loaded after CoPilot is closed. Optional, default is false if the set is not named, true if the set is named.
NotifyUIWhileInside Whether or not to display a fence icon when within the bounds of the fence. Optional, default is false
BeepOnEnterExit Whether or not to sound an audible beep when entering/exiting the bounds of the fence.
Optional, default is false
InnerColor The inner shading of the fence, in the format 0xRRGGBBAA. Optional, default is 0x00afafaf
OuterColor The border color of the fence, in the format 0xRRGGBBAA. Optional, default is 0x000000ff
DrawBelowRoads Whether to draw the fence below roads or on top of them. Optional, default is true

MapObject properties

Property Description Required/Optional
Type Whether the shape is a “Polygon” or a “Line.” Required
Label A label to draw on the map next to the shape. Optional
Name A unique identification name for the fence. Optional

GeofenceMgr.addGeofenceSet

Overview
Description Adds a GeofenceSet to CoPilot.
Supported on React Native Since Version 10.26
Type Method

Syntax

addGeofenceSet(JSONObject)

Parameters GeofenceSet JSONObject - The GeofenceSet JSON Object to add

Return Value None

Sample Code

const GeofenceMgr = NativeModules.GeofenceMgr;
const GeofenceActiveState = NativeModules.GeofenceActiveState;
const GeofenceNotification = NativeModules.GeofenceNotification;
Let geofenceSet = {
name: myGeofenceSet,
 isPersistent : true,
Notification : GeofenceNotification.ENTER
hasUINotification : true,
hasRouteThroughEvent : true,
activeState : GeofenceActiveState.NONE ,
hasBeep : true,
fillColor : 255,
borderColor : 0,
drawUnderRoads : false,
borderWidth : 2,
geofenceList : [
{
Latitude : 40.364992,
Longitude : -74.632334
},
{
Latitude : 40.338631,
Longitude : -74.639373
}.
{
Latitude : 40.352369,
Longitude : -74.689154
},
{
Latitude : 40.368558,
Longitude : -74.670615
}.
}]
};
await GeofenceMgr.addGeofenceSet(geofenceSet);

GeofenceMgr.addGeofences

Overview
Description Adds a list of Geofences to an existing GeofenceSet.
Supported on React Native Since Version 10.26
Type Method

Syntax

addGeofencest(geofenceSetName, geofenceList)

Parameters

geofenceSetName - A string of the name of the GeofenceSet to add Geofences to geofenceList - a JSON array of Geofence Objects

Return Value None

Sample Code

const GeofenceMgr = NativeModules.GeofenceMgr;
const GeofenceActiveState = NativeModules.GeofenceActiveState;
const GeofenceNotification = NativeModules.GeofenceNotification;
var array = [];
array.push({
  "shape": GeofenceShape.CIRCLE,
  "label": mylabel,
  "name": "myname",
  points: [ {
	latitude: 40.361221,
	“‘longitude: -74.599501 },
	{latitude : 40.361221,
 longitude: -74.600563 }]

});
array.push({
  "shape": GeofenceShape.CIRCLE,
  "label": mylabel,
  "name": "myname",
  points: [ {
	latitude: 40.354142,
	“‘longitude: -74.529501 },
	{latitude : 40.35913,
 longitude: -74.610533 }]

});
var sets = await GeofenceMgr.getGeofences();
var setName = sets[0].name;
await GeofenceMgr.addGeofences(setName, array);

GeofenceMgr.getGeofenceSet

Overview
Description Returns an existing GeofenceSet.
Supported on React Native Since Version 10.26
Type Method

Syntax getGeofenceSet(geofenceSetName)

Parameters geofenceSetName - A string of the name of the GeofenceSet to retrieve.

Return Value GeofenceSet JSON Object

Sample Code

const GeofenceMgr = NativeModules.GeofenceMgr;
var sets = await GeofenceMgr.getGeofences();
var geofenceSet = GeofenceMgr.getGeofenceSet(sets[0].name);

GeofenceMgr.getGeofence

Overview
Description Gets an individual, existing Geofence from an existing GeofenceSet.
Supported on React Native Since Version 10.26
Type Method

Syntax

getGeofence(geofenceSetName, geofenceName)

Parameters geofenceSetName - A string of the name of the GeofenceSet to retrieve the Geofence from geofenceName - A string of the name of the Geofence inside the GeofenceSet

Return Value Geofence JSON Object

Sample Code

const GeofenceMgr = NativeModules.GeofenceMgr;
var sets = await GeofenceMgr.getGeofences();
var geofenceSet = GeofenceMgr.getGeofenceSet(sets[0].name);
var geofenceName = geofenceSet.geofences[0].name;
Var geofence = GeofenceMgr.getGeofence(geofenceSet.name, geofenceName);

GeofenceMgr.getGeofences

Overview
Description Gets a list of all of the geofence sets currently in CoPilot.
Supported on React Native Since Version 10.26
Type Method

Syntax

getGeofences()

Parameters None

Return Value

Array of GeofenceSet objects

Sample Code

const GeofenceMgr = NativeModules.GeofenceMgr;
var sets = await GeofenceMgr.getGeofences();

GeofenceMgr.deleteGeofences

Overview
Description Deletes all geofences; a geofence set and its fences; or an individual geofence from CoPilot.
Supported on React Native Since Version 10.26
Type Method

Syntax

deleteGeofences(setName, fenceList);

Parameters setName – A String of the name of the geofence set to delete. If this is an empty string and fenceList is empty or null, all geofences will be deleted. fenceList – An array of names of fences to delete. If this list is empty or null, and setName is valid, everything in that set will be deleted. If this list has multiple elements, each fence in the list in setName will be deleted.

Return Value None

Sample Code

const GeofenceMgr = NativeModules.GeofenceMgr;
var sets = await GeofenceMgr.getGeofences();
var geofencesInSet1 = sets[0].geofences;
//Deleting a single geofence from a geofence set
await GeofenceMgr.deleteGeofence(sets[0].name, [geofencesInSet1[0].name]);
//Deleting all Geofences in a set
await GeofenceMgr.deleteGeofences(sets[0].name), []);
//Deleting all Geofence Sets and Geofences
Await GeofenceMgr.deleteGeofences(“”, null);
Last updated April 19, 2024.
Contents