Skip to content

GeofenceManager

classGeofenceManager

Manages the geofence database — add, remove, query, and check geofences.

Access via BGGeo.instance.geofences. All methods are suspending coroutines (Kotlin) or async/await (Swift).

Methods
Method Description
[add] Add a single geofence.
[addAll] Add multiple geofences in bulk (~10× faster than repeated [add]).
[remove] Remove a geofence by identifier.
[removeAll] Remove all geofences, or a specific subset.
[get] Fetch a single geofence by identifier.
[getAll] Fetch all stored geofences.
[exists] Check whether a geofence with a given identifier exists.

Members

add

suspend fun add(geofence:Geofence)

Add a Geofence to be monitored by the native geofencing API.

Note

If a geofence with the same Geofence.identifier already exists, it is deleted before the new one is inserted. When adding multiple geofences, addGeofences is approximately 10× faster.

See also - 📘 Geofencing Guide

// Within a coroutine scope
val bgGeo = BGGeo.instance

val geofence = Geofence.Builder()
    .setIdentifier("Home")
    .setRadius(150f)
    .setLatitude(45.51921926)
    .setLongitude(-73.61678581)
    .setNotifyOnEntry(true)
    .setNotifyOnExit(false)
    .setNotifyOnDwell(true)
    .setLoiteringDelay(30000)  // 30 seconds
    .setExtras(mapOf("zone_id" to 1234))
    .build()

try {
    bgGeo.geofences.add(geofence)
    Log.d(TAG, "[addGeofence] success")
} catch (e: Exception) {
    Log.d(TAG, "[addGeofence] FAILURE: $e")
}

addAll

suspend fun addAll(geofences: List<Geofence>)

Add a list of Geofence to be monitored by the native geofencing API.

Note

If any geofence already exists with a matching Geofence.identifier, it is deleted before the new one is inserted.

See also - 📘 Geofencing Guide - addGeofence

// Within a coroutine scope
val bgGeo = BGGeo.instance

val geofences = listOf(
    Geofence.Builder()
        .setIdentifier("foo")
        .setRadius(200f)
        .setLatitude(45.51921926)
        .setLongitude(-73.61678581)
        .setNotifyOnEntry(true)
        .build(),
    Geofence.Builder()
        .setIdentifier("bar")
        .setRadius(200f)
        .setLatitude(45.51921926)
        .setLongitude(-73.61678581)
        .setNotifyOnEntry(true)
        .build()
)

bgGeo.geofences.addAll(geofences)

exists

suspend fun exists(identifier: String): Boolean

Determine whether a geofence with the given identifier exists in the SDK's database.

See also - 📘 Geofencing Guide

// Within a coroutine scope
val bgGeo = BGGeo.instance

val exists = bgGeo.geofences.exists("HOME")
Log.d(TAG, "[geofenceExists] $exists")

get

suspend fun get(identifier: String):Geofence

Fetch a single Geofence by identifier from the SDK's database.

See also - 📘 Geofencing Guide

// Within a coroutine scope
val bgGeo = BGGeo.instance

val geofence = bgGeo.geofences.get("HOME")
Log.d(TAG, "[getGeofence] $geofence")

getAll

suspend fun getAll(): List<Geofence>

Fetch all Geofence records from the SDK's database.

Returns an empty array if no geofences are stored.

See also - 📘 Geofencing Guide

// Within a coroutine scope
val bgGeo = BGGeo.instance

val geofences = bgGeo.geofences.getAll()
Log.d(TAG, "[getGeofences] $geofences")

remove

suspend fun remove(identifier: String)

Remove the Geofence with the given Geofence.identifier.

See also - 📘 Geofencing Guide

// Within a coroutine scope
val bgGeo = BGGeo.instance

try {
    bgGeo.geofences.remove("Home")
    Log.d(TAG, "[removeGeofence] success")
} catch (e: Exception) {
    Log.d(TAG, "[removeGeofence] FAILURE: $e")
}

removeAll

suspend fun removeAll(identifiers: List<String>? = null)

Remove all monitored Geofence records, or a specific subset by identifier.

See also - 📘 Geofencing Guide

// Within a coroutine scope
val bgGeo = BGGeo.instance

bgGeo.geofences.removeAll()