Monitor POIs with Geofence

Monitor POIs with Geofence using Flutter Plugin

  1. Create and monitor Geofences
  2. Adding and removing regions
  3. 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:

Create a custom circle region

dart
        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

dart
        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.

dart
        Future<String?> returnVal = geofencingFlutterPlugin.removeRegions('7F91369E-467C-4CBD-8D41-6509815C4780');

returnVal.then((value){
    debugPrint(value!);
}).catchError((error) {
    debugPrint('An error occurred: ${error.message}');
});

    

or

dart
        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.

dart
        //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.

dart
        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:

dart
        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.

Was this article helpful?
Have more questions? Submit a request