Skip to content
Chris Scott edited this page Aug 16, 2017 · 3 revisions

The plugin includes native geofencing features. You may add, remove and query the list of monitored geofences from the native plugin. The native plugin will persist monitored geofences and re-initiate them when the app boots or the device is restarted.

You can demo these features right now using the SampleApp.

Geofence Features

A monitored geofence will remain active until you explicity remove it via bgGeo.removeGeofence(identifier).

Geofence Model

@param {String} identifier

A unique String to identify your Geofence, eg: "Home", "Office".

@param {Integer} radius

The radius of the circular geofence. A radius of >100 meters works best.

@param {Boolean} notifyOnEntry

Transitioning into the geofence will generate an event.

@param {Boolean} notifyOnExit

Transitioning out of the geofence will generate an event.

Listening to Geofence Events

Listen to geofence transition events using the method #onGeofence. You may set up any number of #onGeofence event-listeners throughout your code -- they will all be executed.

Add Geofence

    bgGeo.addGeofence({
        identifier: "Home",
        radius: 200,
        latitude: 47.2342323,
        longitude: -57.342342,
        notifyOnEntry: true
    });
    
    bgGeo.onGeofence(function(geofence, taskId) {
        try {
            var identifier = geofence.identifier;
            var action     = geofence.action;
            var location   = geofence.location;

            console.log("- A Geofence transition occurred");
            console.log("  identifier: ", identifier);
            console.log("  action: ", action);
            console.log("  location: ", JSON.stringify(location));
        } catch(e) {
            console.error("An error occurred in my code!", e);
        }
        // Be sure to call #finish!!
        bgGeo.finish(taskId);
    });

Removing Geofences

The native plugin will continue to monitor geofences and fire transition-events until you explicity tell the plugin to remove a geofence via #removeGeofence(identifier).

    bgGeo.removeGeofence("Home");

Querying Geofences

The native plugin persists monitored geofences between application boots and device restarts. When your app boots, you can fetch the currently monitored geofences from the native plugin and, for example, re-draw markers on your map.

    bgGeo.getGeofences(function(geofences) {
        for (var n=0,len=geofences.length;n<len;n++) {
            var geofence = geofences[n];
            var marker = new google.maps.Circle({
                radius: parseInt(geofence.radius, 10),
                center: new google.maps.LatLng(geofence.latitude, geofence.longitude),
                map: myMapInstance
            });
        }
    });