Back

GeoFence


The plugin allows you to define perimeters, also referred to as geofences, which surround the areas of interest. Your app gets a notification when the device crosses a geofence, which allows you to provide a useful experience when users are in the vicinity.

To use GeoFence plugin, you must first load the plugin at the top of your script using the LoadPlugin method of the app object like this:

 app.LoadPlugin( "GeoFence" )

Once you have the plugin loaded, you can create an instance of the GeoFence plugin by calling the CreateGeoFence method of the app object like this:

For polygon boundary:

 app.CreateGeoFence()

Methods

 AddPolygon(path, name)
 SetPolygon(newPath, name)
 AddCircle(lat, lng, radius, name)
 SetCircle(newLat, newLng, newRadius, name)
 SetCenter(newLat, newLng, name)
 SetRadius(newRadius)
 SetOnChange(callback)
 Start()
 Stop()
 ClearWatch(name)

Definitions


AddPolygon(path, name)

This method will add a new polygon geofence with a name of name.
path Array
Required. An array of latLng literal objects for the series of points on a polygon
name String
Required. The name of the polygon geofence

Sample Path

    // This path creates a polygon with four points
    var path = [
        {lat: 65.692266, lng: -26.712499},
        {lat: 65.836578, lng: 65.748431},
        {lat: 27.600915, lng: 65.045306},
        {lat: 25.244269, lng: -21.096074}
    ]
Copy

SetPolygon(newPath, name)

This method will set a new path for an existing polygon geofence. A polygon geofence with a name of name must exist
path Array
Required. The new array of latLng literal objects for the new path of an existing polygon
name String
Required. The name of the polygon geofence to be set

AddCircle(lat, lng, radius, name)

This method will add a new circle geofence with a name of name.
lat Number
Required. The latitude of the center
lng Number
Required. The longitude of the center
radius Number
Required. The radius in meters
name String
Required. The name of the circle geofence

SetCircle(newLat, newLng, newRadius, name)

This method will set a new boundary for an existing circle geofence. A circle geofence with a name of name must exist
newLat Number
Required. The new latitude of the center
newLng Number
Required. The new longitude of the center
newRadius Number
Required. The new radius in meters
name String
Required. The name of the circle geofence to be set

SetCenter(newLat, newLng, name)

This method will set a new center for an existing circle geofence. A circle geofence with a name of name must exist
newLat Number
Required. The new latitude of the center
newLng Number
Required. The new longitude of the center
name String
Required. The name of the circle geofence to be set

SetRadius(newRadius, name)

This method will set a new radius for an existing circle geofence. A circle geofence with a name of name must exist
newRadius Number
Required. The new radius in meters.
name String
Required. The name of the circle geofence to be set

SetOnChange(callback)

Sets a function to be called when there's a change of value to any of the defined geofences.
It will pass the value, name respectively of the callback
value : Boolean. Can be true or false value : String. The name of the geofence for the corresponding value
callback Function
Required. The function to be called when there's a change of value.
Returns:
true for entering (inside) or
false for leaving (outside)

Start()

This method will start the watching of the geofences

Stop()

This method will stop the watching of the geofences.

ClearWatch(name)

This method will clear the watch of the specific geofence of name name. If no name is provided, all the watchlist is cleared and the geofencing will be stop.

Polygon sample

app.LoadPlugin("GeoFence")

function OnStart()
{   
    var path = [
        {lat: 65.692266, lng: -26.712499},
        {lat: 65.836578, lng: 65.748431},
        {lat: 27.600915, lng: 65.045306},
        {lat: 25.244269, lng: -21.096074}
    ]
    fence = app.CreateGeoFence()
    fence.AddPolygon(path, "FirstFence")
    fence.SetOnChange(OnFenceChange)
    fence.Start()
}

function OnFenceChange(val, name)
{
    if(val) app.ShowPopup("You are entering the boundary of " + name)
    else app.ShowPopup("You are leaving the boundary of" + name)
}
Copy    Run   

Circle sample

app.LoadPlugin("GeoFence");

function OnStart()
{
    var lat = 46.979536, lng = 2.215101, radius = 1000000       //1000 kilometers from the center of FRANCE
    fence = app.CreateGeoFence()
    fence.AddCircle(lat, lng, radius, "CircleFence")
    fence.SetOnChange(OnFenceChange)
    fence.Start()
}

function OnFenceChange(val, name)
{
    if(val) app.ShowPopup("You are entering the boundary" + name)
    else app.ShowPopup("You are leaving the boundary" + name)
}
Copy    Run   

More than one fence

app.LoadPlugin("GeoFence")

function OnStart()
{
    var lat = 46.979536, lng = 2.215101, radius = 1000000       //1000 kilometers from the center of FRANCE
    var path = [
        {lat: 65.692266, lng: -26.712499},
        {lat: 65.836578, lng: 65.748431},
        {lat: 27.600915, lng: 65.045306},
        {lat: 25.244269, lng: -21.096074}
    ]

    fence = app.CreateGeoFence()
    fence.AddPolygon(path, "PolygonFence")
    fence.AddCircle(lat, lng, radius, "CircleFence")
    fence.SetOnChange(OnFenceChange)
    fence.Start()
}

function OnFenceChange(val, name)
{
    if(val && name == "PolygonFence")
    {
        app.ShowPopup("You are entering the Polygon fence")
    }
    else if(val && name == "CircleFence")
    {
        app.ShowPopup("You are entering the Circle fence")
    }
}
Copy    Run