/// <summary> /// Adds the geofences. /// </summary> private void AddGeofences() { try { var geofenceList = new List <Android.Gms.Location.IGeofence>(); var regions = this.Regions.Values; foreach (GeofenceCircularRegion region in regions) { int transitionTypes = 0; if (region.NotifyOnStay) { transitionTypes |= Android.Gms.Location.Geofence.GeofenceTransitionDwell; } if (region.NotifyOnEntry) { transitionTypes |= Android.Gms.Location.Geofence.GeofenceTransitionEnter; } if (region.NotifyOnExit) { transitionTypes |= Android.Gms.Location.Geofence.GeofenceTransitionExit; } if (transitionTypes != 0) { geofenceList.Add(new Android.Gms.Location.GeofenceBuilder() .SetRequestId(region.Id) .SetCircularRegion(region.Latitude, region.Longitude, (float)region.Radius) .SetLoiteringDelay((int)region.StayedInThresholdDuration.TotalMilliseconds) ////.SetNotificationResponsiveness(mNotificationResponsivness) .SetExpirationDuration(Android.Gms.Location.Geofence.NeverExpire) .SetTransitionTypes(transitionTypes) .Build()); if (GeofenceStore.SharedInstance.Get(region.Id) == null) { GeofenceStore.SharedInstance.Save(region); } CrossGeofence.GeofenceListener.OnMonitoringStarted(region.Id); } } if (geofenceList.Count > 0) { Android.Gms.Location.GeofencingRequest request = new Android.Gms.Location.GeofencingRequest.Builder().SetInitialTrigger(Android.Gms.Location.GeofencingRequest.InitialTriggerEnter).AddGeofences(geofenceList).Build(); Android.Gms.Location.LocationServices.GeofencingApi.AddGeofences(this.modeGoogleApiClient, request, this.GeofenceTransitionPendingIntent).SetResultCallback(this); this.CurrentRequestType = RequestType.Default; } } catch (Java.Lang.Exception ex1) { string message = string.Format("{0} - Error: {1}", CrossGeofence.Id, ex1); System.Diagnostics.Debug.WriteLine(message); CrossGeofence.GeofenceListener.OnError(message); } catch (Exception ex2) { string message = string.Format("{0} - Error: {1}", CrossGeofence.Id, ex2); System.Diagnostics.Debug.WriteLine(message); CrossGeofence.GeofenceListener.OnError(message); } }
private void AddGeofences() { try { List<Android.Gms.Location.IGeofence> geofenceList = new List<Android.Gms.Location.IGeofence>(); var regions = Regions.Values; foreach (GeofenceCircularRegion region in regions) { int transitionTypes = 0; if (region.NotifyOnStay) { transitionTypes |= Android.Gms.Location.Geofence.GeofenceTransitionDwell; } if (region.NotifyOnEntry) { transitionTypes |= Android.Gms.Location.Geofence.GeofenceTransitionEnter; } if (region.NotifyOnExit) { transitionTypes |= Android.Gms.Location.Geofence.GeofenceTransitionExit; } if (transitionTypes != 0) { geofenceList.Add(new Android.Gms.Location.GeofenceBuilder() .SetRequestId(region.Id) .SetCircularRegion(region.Latitude, region.Longitude, (float)region.Radius) .SetLoiteringDelay((int)region.StayedInThresholdDuration.TotalMilliseconds) //.SetNotificationResponsiveness(mNotificationResponsivness) .SetExpirationDuration(Android.Gms.Location.Geofence.NeverExpire) .SetTransitionTypes(transitionTypes) .Build()); if (GeofenceStore.SharedInstance.Get(region.Id) == null) { GeofenceStore.SharedInstance.Save(region); } CrossGeofence.GeofenceListener.OnMonitoringStarted(region.Id); } } if (geofenceList.Count > 0) { Android.Gms.Location.GeofencingRequest request = new Android.Gms.Location.GeofencingRequest.Builder().SetInitialTrigger(Android.Gms.Location.GeofencingRequest.InitialTriggerEnter).AddGeofences(geofenceList).Build(); Android.Gms.Location.LocationServices.GeofencingApi.AddGeofences(mGoogleApiClient, request, GeofenceTransitionPendingIntent).SetResultCallback(this); CurrentRequestType = RequestType.Default; } } catch (Java.Lang.Exception ex1) { string message = string.Format("{0} - Error: {1}", CrossGeofence.Id, ex1.ToString()); System.Diagnostics.Debug.WriteLine(message); CrossGeofence.GeofenceListener.OnError(message); } catch (System.Exception ex2) { string message = string.Format("{0} - Error: {1}", CrossGeofence.Id, ex2.ToString()); System.Diagnostics.Debug.WriteLine(message); CrossGeofence.GeofenceListener.OnError(message); } }