public void RegisterGeofences(PlaceGeofence[] fenceData) { if(pendingIntent != null) { Toast.MakeText(context, "Removing old fences", ToastLength.Short).Show(); currentStage = FencingStage.Removing; } if (fencesToAdd == null) { fencesToAdd = new List<IGeofence>(); } ISharedPreferences prefs = context.GetSharedPreferences("FENCES", FileCreationMode.MultiProcess); ISharedPreferencesEditor editor = prefs.Edit(); editor.Clear(); editor.Apply(); foreach (PlaceGeofence fence in fenceData) { fencesToAdd.Add(new GeofenceBuilder() .SetCircularRegion(fence.lat, fence.lng, fence.radius) .SetExpirationDuration(Geofence.NeverExpire) .SetRequestId(fence.placeId) .SetLoiteringDelay(0) .SetTransitionTypes(Geofence.GeofenceTransitionEnter | Geofence.GeofenceTransitionDwell | Geofence.GeofenceTransitionExit) .Build()); editor.PutString(fence.placeId, JsonConvert.SerializeObject(fence)); } editor.Apply(); if(googleApiClient == null) { intent = new Intent(context, typeof(GeofencingReceiver)); googleApiClient = new GoogleApiClientBuilder(context) .AddApi(LocationServices.Api) .AddConnectionCallbacks(this) .AddOnConnectionFailedListener(this) .Build(); } googleApiClient.Connect(); }
private void AddFences() { pendingIntent = CreatePendingIntent(); GeofencingRequest fenceReq = new GeofencingRequest.Builder().AddGeofences(fencesToAdd).Build(); currentStage = FencingStage.Adding; var result = LocationServices.GeofencingApi.AddGeofences(googleApiClient, fenceReq, pendingIntent); result.SetResultCallback(this); }