Wednesday, July 29, 2015

Android Geo Fencing Basics

Geo fencing work by marking a lat and long as the center and a radius as the proximity. Or in other words, the latitude, longitude and radius define a geofence. 
There can be multiple geo fences active with a limit of 100 per device user. For each geofence, we can ask location services to send the entrance and exit events or can specify the duration within the geo fence area to wait or dwell before triggering an event. We can limit the duration of any geo fence by specifying an expiration duration in milliseconds. Afer the geofence expires, Location services automatically removes it. 

Set up for Geo fence Monitoring 
Ignored for this to work, application needs to have ACCESS_FINE_LOCATION permission in the application manifest file. 

Ignored to listen to the geo fence transitions, need to have a geo fence intent service in the manifest file as well. 
Below is what taken from the application manifest for e.g. 

To Create the Geo fence, Application needs to use the Builder class available in the location APIs.To handle the intents sent from Location services when geofence transitions occur, application can specify a PendingIntent  

Below are the main steps involved 

1. Define Geofence
2. Monitory State changes 

Defining the Geofence is in following 3 three steps 
1. Create Geofence objects by specifying lat, long, radius, Id, expiration duration, transition types. Use GeoFence.Builder for this
2. Make a geofencing request - Use GeoFencingRequest.Builder for this purpose. need to add the geo fences created in step #1 to this and set the Initial triggers 
3. Define an intent for the geo fencing transitions - This is a normal PendingIntent 
4. Now add the geo fences with the configurations to the GeoFencingApi 

LocationServices.GeoFencingApi.addGeoFences(mGoogleApiClient, GeoFencingRequestObject, GeoFencingPendingIntent).setResultCallback(this);

References:

No comments:

Post a Comment