Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@ class GeofencingPlugin : ActivityAware, FlutterPlugin, MethodCallHandler {
}
}

@JvmStatic
private fun getRegisteredGeofences(context: Context, result: Result) {
synchronized(sGeofenceCacheLock) {
val list = ArrayList<Map<String,String?>>()
var p = context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
var persistentGeofences = p.getStringSet(PERSISTENT_GEOFENCES_IDS, null)
if (persistentGeofences != null && persistentGeofences.size > 0) {
for (id in persistentGeofences) {
var persistentGeofencesData = (p.getString(getPersistentGeofenceKey(id), null))?.split(",");
var id = persistentGeofencesData?.get(1)
var lat = persistentGeofencesData?.get(2)
var long = persistentGeofencesData?.get(3)
var radius = persistentGeofencesData?.get(4)

val dataMap = mapOf("id" to id, "lat" to lat, "long" to long, "radius" to radius)
list.add(dataMap);

}
}
result.success(list)
}
}


@JvmStatic
private fun removeGeofenceFromCache(context: Context, id: String) {
synchronized(sGeofenceCacheLock) {
Expand Down Expand Up @@ -271,6 +295,7 @@ class GeofencingPlugin : ActivityAware, FlutterPlugin, MethodCallHandler {
args,
result)
"GeofencingPlugin.getRegisteredGeofenceIds" -> getRegisteredGeofenceIds(mContext!!, result)
"GeofencingPlugin.getRegisteredGeofenceRegions" -> getRegisteredGeofences(mContext!!, result)
else -> result.notImplemented()
}
}
Expand Down
25 changes: 22 additions & 3 deletions ios/Classes/GeofencingPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
result(@([self removeGeofence:arguments]));
} else if ([@"GeofencingPlugin.getRegisteredGeofenceIds" isEqualToString:call.method]) {
result([self getMonitoredRegionIds:arguments]);
}
else {
} else if ([@"GeofencingPlugin.getRegisteredGeofenceRegions" isEqualToString:call.method]) {
result([self getMonitoredRegions:arguments]);
} else {
result(FlutterMethodNotImplemented);
}
}
Expand Down Expand Up @@ -210,14 +211,32 @@ - (BOOL)removeGeofence:(NSArray *)arguments {
return NO;
}

-(NSArray*)getMonitoredRegionIds:()arguments{
- (NSArray*)getMonitoredRegionIds:()arguments{
NSMutableArray *geofenceIds = [[NSMutableArray alloc] init];
for (CLRegion *region in [self->_locationManager monitoredRegions]) {
[geofenceIds addObject:region.identifier];
}
return [NSArray arrayWithArray:geofenceIds];
}

- (NSArray*)getMonitoredRegions:()arguments{
NSMutableArray *geofences = [[NSMutableArray alloc] init];
for (CLCircularRegion *region in [self->_locationManager monitoredRegions]) {

NSString *latitude = [[NSNumber numberWithDouble:region.center.latitude] stringValue];
NSString *longitude = [[NSNumber numberWithDouble:region.center.longitude] stringValue];
NSString *radius = [[NSNumber numberWithDouble:region.radius] stringValue];
id objects[] = {region.identifier, latitude, longitude, radius};
id keys[] = {@"id", @"lat", @"long", @"radius"};
NSUInteger count = sizeof(objects) / sizeof(id);

NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:count];
[geofences addObject:dict];
}
return [NSArray arrayWithArray:geofences];

}

- (int64_t)getCallbackDispatcherHandle {
id handle = [_persistentState objectForKey:@"callback_dispatcher_handle"];
if (handle == nil) {
Expand Down
13 changes: 13 additions & 0 deletions lib/src/geofencing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ class GeofencingManager {
List<String>.from(await _channel
.invokeMethod('GeofencingPlugin.getRegisteredGeofenceIds'));

/// get all geofence regions and their properties
/// returns a [Map] with the following keys
/// [id] the identifier
/// [lat] latitude
/// [long] longitude
/// [radius] radius
///
/// if there are no geofences registered it returns []
static Future<List<Map<dynamic, dynamic>>>
getRegisteredGeofenceRegions() async =>
List<Map<dynamic, dynamic>>.from(await _channel
.invokeMethod('GeofencingPlugin.getRegisteredGeofenceRegions'));

/// Stop receiving geofence events for a given [GeofenceRegion].
static Future<bool> removeGeofence(GeofenceRegion region) async =>
(region == null) ? false : await removeGeofenceById(region.id);
Expand Down