Monitor POIs with Geofence
Monitor POIs with Geofence using Flutter Plugin
- Create and monitor Geofences
- Adding and removing regions
- Create regions from Woosmap Search API assets
Create and monitor Geofences
Use region monitoring to determine when the user enters or leaves a geographic region.
Region monitoring (also known as Geofencing) combines awareness of the user’s current location with awareness of the user’s proximity to locations that may be of interest. This region alerts your app when the user enters or exits a geographical region. To mark a location of interest, you specify its latitude and longitude. To adjust the proximity for the location, you add a radius. The latitude, longitude, and radius define a Geofence, creating a circular area, or fence, around the location of interest. Find more details about Geofences in Geofence documentation
Adding and removing regions
Call addRegion
method to add a region you want to monitor. Region type can be circle
or isochrone
only. This method will accept an object with following attributes:
- regionId - Id of the region
- lat - Latitude
- lng - Longitude
- radius - Radius in meters
- type - type of region
Create a custom circle region
GeofenceRegion geofenceRegion = GeofenceRegion(
'7F91369E-467C-4CBD-8D41-6509815C4780',
51.50998,
-0.1337,
180,
RegionType.circle
);
Future<String?> returnVal = geofencingFlutterPlugin.addRegion(geofenceRegion);
returnVal.then((value){
debugPrint(value!);
}).catchError((error) {
debugPrint('An error occurred: ${error.message}');
});
Create a custom isochrone region
GeofenceRegion geofenceRegion = GeofenceRegion(
'7F91369E-467C-4CBD-8D41-6509815C4780',
51.50998,
-0.1337,
180,
RegionType.isochrone
);
Future<String?> returnVal = geofencingFlutterPlugin.addRegion(geofenceRegion);
returnVal.then((value){
debugPrint(value!);
}).catchError((error) {
debugPrint('An error occurred: ${error.message}');
});
Remove regions
Call removeRegions
method to remove a region that you are monitoring. This method will accept the following parameter, and passing a null value will remove all regions.
- regionId - Id of the region
- lat - Latitude
- lng - Longitude
- radius - Radius in meters
Future<String?> returnVal = geofencingFlutterPlugin.removeRegions('7F91369E-467C-4CBD-8D41-6509815C4780');
returnVal.then((value){
debugPrint(value!);
}).catchError((error) {
debugPrint('An error occurred: ${error.message}');
});
or
Future<String?> returnVal = geofencingFlutterPlugin.removeRegions();
returnVal.then((value){
debugPrint(value!);
}).catchError((error) {
debugPrint('An error occurred: ${error.message}');
});
Whenever user crosses boundary of one of your app’s registered regions, the system notifies your app.
On object Region
, there is a boolean didEnter
that indicates if you enter or exit region. You have
another boolean fromPositionDetection
to know if the detection was launched by the position detection or by the system
detection.
Regions have an associated identifier, which this method uses to look up information related to region and perform associated action.
Get regions from the local database
Call getRegions
method to get an array of Regions from local db.
//Get a single region
Future<List<Region>?> returnVal = geofencingFlutterPlugin.getRegions(regionId);
returnVal.then((regions){
if (regions != null){
debugPrint('Regions: ${regions.length}');
}else{
debugPrint('Regions is null');
}
}).catchError((error) {
debugPrint('An error occurred: ${error.message}');
});
//Get all regions
Future<List<Region>?> returnVal = geofencingFlutterPlugin.getRegions();
returnVal.then((regions){
if (regions != null){
debugPrint('Regions: ${regions.length}');
}else{
debugPrint('Regions is null');
}
}).catchError((error) {
debugPrint('An error occurred: ${error.message}');
});
Watch Region to track the region’s events
Call watchRegions
method to track Regions. This method will invoke a callback with the Region object.
Future<String?> returnVal = geofencingFlutterPlugin.watchRegion();
returnVal.then((value){
//Get the region stream
watchRegionStream = geofencingFlutterPlugin.getWatchRegionStream();
//Listen to the stream
watchRegionStream.listen((region){
//Region updates will be received here.
if (location != null) {
debugPrint(region.eventName);
} else {
debugPrint("Region is null");
}
});
}).catchError((error) {
debugPrint('An error occurred: $error');
});
To remove watch:
Future<String?> returnVal = geofencingFlutterPlugin.clearRegionWatch();
returnVal.then((value){
debugPrint(value!);
}).catchError((error) {
debugPrint('An error occurred: $error');
});
Create regions from Woosmap Search API assets
Region monitoring is a natural complement to Search requests performed on collected locations. Indeed, Search requests help monitor approach to some assets you want to monitor. On every collected location you are aware of surrounding assets (distance to them and even time if using Distance API request). You can then decide to monitor some surrounding assets (e.g. the closest ones). Region monitoring is designed to do so.
To create a region around nearest result of the Search API request, choose a tracking profile with the tracking
properties searchAPICreationRegionEnable
enable.
Pre-requisites
You must define a Woosmap private API key to request the Woosmap Search API. How to set an Woosmap private key is explained
in
the Initializing the plugin section.