Skip to content

Instantly share code, notes, and snippets.

@tugcearar
Created September 10, 2020 14:59
Show Gist options
  • Save tugcearar/0ce3bc082edbf350a4c6bd189f2babe7 to your computer and use it in GitHub Desktop.
Save tugcearar/0ce3bc082edbf350a4c6bd189f2babe7 to your computer and use it in GitHub Desktop.
class GeofenceManager
{
public MainActivity activity;
public GeofenceManager(MainActivity activity)
{
this.activity = activity;
}
private PendingIntent CreatePendingIntent()
{
Intent intent = new Intent(activity, typeof(GeofenceBroadcastReceiver));
intent.SetAction(GeofenceBroadcastReceiver.ActionGeofence);
return PendingIntent.GetBroadcast(activity, 0, intent, PendingIntentFlags.UpdateCurrent);
}
public void AddGeofences(GeofenceModel geofenceModel)
{
//Set parameters
geofenceModel.Id = Guid.NewGuid().ToString();
if (geofenceModel.Conversion == 5) //Expiration value that indicates the geofence should never expire.
geofenceModel.Timeout = Geofence.GeofenceNeverExpire;
else
geofenceModel.Timeout = 10000;
List<IGeofence> geofenceList = new List<IGeofence>();
//Geofence Service
GeofenceService geofenceService = LocationServices.GetGeofenceService(activity);
PendingIntent pendingIntent = CreatePendingIntent();
GeofenceBuilder somewhereBuilder = new GeofenceBuilder()
.SetUniqueId(geofenceModel.Id)
.SetValidContinueTime(geofenceModel.Timeout)
.SetRoundArea(geofenceModel.LatLng.Latitude, geofenceModel.LatLng.Longitude, geofenceModel.Radius)
.SetDwellDelayTime(10000)
.SetConversions(geofenceModel.Conversion); ;
//Create geofence request
geofenceList.Add(somewhereBuilder.Build());
GeofenceRequest geofenceRequest = new GeofenceRequest.Builder()
.CreateGeofenceList(geofenceList)
.Build();
//Register geofence
var geoTask = geofenceService.CreateGeofenceList(geofenceRequest, pendingIntent);
geoTask.AddOnSuccessListener(new CreateGeoSuccessListener(activity));
geoTask.AddOnFailureListener(new CreateGeoFailListener(activity));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment