public void AddRegion(GeofenceCircularRegion region) { if (string.IsNullOrEmpty(region.Id)) return; RemoveRegion(region.Id); var position = new BasicGeoposition(); position.Latitude = region.Latitude; position.Longitude = region.Longitude; var geocircle = new Geocircle(position, region.Radius); Windows.Devices.Geolocation.Geofencing.MonitoredGeofenceStates mask = 0; if (region.NotifyOnEntry) { mask |= Windows.Devices.Geolocation.Geofencing.MonitoredGeofenceStates.Entered; } if (region.NotifyOnExit) { mask |= Windows.Devices.Geolocation.Geofencing.MonitoredGeofenceStates.Exited; } var geofence = new Windows.Devices.Geolocation.Geofencing.Geofence(region.Id, geocircle, mask, false, new TimeSpan(0, 0, CrossGeofence.StayedInDuration / 1000), DateTime.Now, TimeSpan.MaxValue); Windows.Devices.Geolocation.Geofencing.GeofenceMonitor.Current.Geofences.Add(geofence); GeofenceStore.SharedInstance.Save(region); }
/// <summary> /// Save a geofence /// </summary> /// <param name="region">The GeofenceCircularRegion with the values you want to save in SharedPreferemces</param> public override void Save(GeofenceCircularRegion region) { if (!region.Persistent) return; string id = region.Id; // Get a SharedPreferences editor instance. Among other things, SharedPreferences ensures that updates are atomic and non-concurrent ISharedPreferencesEditor prefs = mPrefs.Edit(); // Write the geofence values to SharedPreferences prefs.PutFloat(GetFieldKey(id, LatitudeGeofenceRegionKey), (float)region.Latitude); prefs.PutFloat(GetFieldKey(id, LongitudeGeofenceRegionKey), (float)region.Longitude); prefs.PutFloat(GetFieldKey(id, RadiusGeofenceRegionKey), (float)region.Radius); prefs.PutBoolean(GetFieldKey(id, NotifyOnEntryGeofenceRegionKey), region.NotifyOnEntry); prefs.PutBoolean(GetFieldKey(id, NotifyOnExitGeofenceRegionKey), region.NotifyOnExit); prefs.PutBoolean(GetFieldKey(id, NotifyOnStayGeofenceRegionKey), region.NotifyOnStay); prefs.PutString(GetFieldKey(id, NotificationEntryMessageGeofenceRegionKey), region.NotificationEntryMessage); prefs.PutString(GetFieldKey(id, NotificationExitMessageGeofenceRegionKey), region.NotificationExitMessage); prefs.PutString(GetFieldKey(id, NotificationStayMessageGeofenceRegionKey), region.NotificationStayMessage); prefs.PutBoolean(GetFieldKey(id, ShowNotificationGeofenceRegionKey), region.ShowNotification); prefs.PutBoolean(GetFieldKey(id, PersistentGeofenceRegionKey), region.Persistent); prefs.PutInt(GetFieldKey(id, StayedInThresholdDurationGeofenceRegionKey), (int)region.StayedInThresholdDuration.TotalMilliseconds); // Commit the changes prefs.Commit (); }
public override void Save(GeofenceCircularRegion region) { string id = region.Id; Container.Values[GetFieldKey(id, IdGeofenceRegionKey)] = region.Id; Container.Values[GetFieldKey(id, LatitudeGeofenceRegionKey)] = region.Latitude; Container.Values[GetFieldKey(id, LongitudeGeofenceRegionKey)] = region.Longitude; Container.Values[GetFieldKey(id, NotifyOnEntryGeofenceRegionKey)] = region.NotifyOnEntry; Container.Values[GetFieldKey(id, NotifyOnExitGeofenceRegionKey)] = region.NotifyOnExit; Container.Values[GetFieldKey(id, RadiusGeofenceRegionKey)] = region.Radius; }
public GeofenceCircularRegion(GeofenceCircularRegion region) { Id = region.Id; Latitude = region.Latitude; Longitude = region.Longitude; Radius = region.Radius; NotifyOnEntry = region.NotifyOnEntry; NotifyOnExit = region.NotifyOnExit; NotifyOnStay = region.NotifyOnStay; ShowNotification = region.ShowNotification; Persistent = region.Persistent; ShowEntryNotification = region.ShowEntryNotification; ShowExitNotification = region.ShowExitNotification; ShowStayNotification = region.ShowStayNotification; }
public override GeofenceCircularRegion Get(string id) { GeofenceCircularRegion region=null; if (Container.Values.ContainsKey(GetFieldKey(id, IdGeofenceRegionKey)) && Container.Values.ContainsKey(GetFieldKey(id, LatitudeGeofenceRegionKey)) && Container.Values.ContainsKey(GetFieldKey(id, LongitudeGeofenceRegionKey)) && Container.Values.ContainsKey(GetFieldKey(id, NotifyOnEntryGeofenceRegionKey)) && Container.Values.ContainsKey(GetFieldKey(id, NotifyOnExitGeofenceRegionKey)) && Container.Values.ContainsKey(GetFieldKey(id, RadiusGeofenceRegionKey))) { region= new GeofenceCircularRegion() { Id = Container.Values[GetFieldKey(id, IdGeofenceRegionKey)].ToString(), Latitude = (double)Container.Values[GetFieldKey(id, LatitudeGeofenceRegionKey)], Longitude = (double)Container.Values[GetFieldKey(id, LongitudeGeofenceRegionKey)], Radius = (double)Container.Values[GetFieldKey(id, RadiusGeofenceRegionKey)], NotifyOnEntry = (bool)Container.Values[GetFieldKey(id, NotifyOnEntryGeofenceRegionKey)], NotifyOnExit = (bool)Container.Values[GetFieldKey(id, NotifyOnExitGeofenceRegionKey)], }; } return region; }
/// <summary> /// Starts monitoring specified region /// </summary> /// <param name="region"></param> public void StartMonitoring(GeofenceCircularRegion region) { /*if (IsMonitoring && mGoogleApiClient.IsConnected) { Android.Gms.Location.LocationServices.GeofencingApi.RemoveGeofences(mGoogleApiClient, GeofenceTransitionPendingIntent).SetResultCallback(this); }*/ if (!mRegions.ContainsKey(region.Id)) { mRegions.Add(region.Id, region); } RequestMonitoringStart(); }
public override GeofenceCircularRegion Get(string id) { GeofenceCircularRegion region = null; if(!string.IsNullOrEmpty(NSUserDefaults.StandardUserDefaults.StringForKey(GetFieldKey(id, IdGeofenceRegionKey)))) { double lat = NSUserDefaults.StandardUserDefaults.DoubleForKey(GetFieldKey(id, LatitudeGeofenceRegionKey)); double lon = NSUserDefaults.StandardUserDefaults.DoubleForKey(GetFieldKey(id, LongitudeGeofenceRegionKey)); bool notifyOnEntry = NSUserDefaults.StandardUserDefaults.BoolForKey(GetFieldKey(id, NotifyOnEntryGeofenceRegionKey)); bool notifyOnExit = NSUserDefaults.StandardUserDefaults.BoolForKey(GetFieldKey(id, NotifyOnExitGeofenceRegionKey)); bool notifyOnStay = NSUserDefaults.StandardUserDefaults.BoolForKey(GetFieldKey(id, NotifyOnStayGeofenceRegionKey)); double radius = NSUserDefaults.StandardUserDefaults.DoubleForKey(GetFieldKey(id, RadiusGeofenceRegionKey)); string notificationEntryMessage = NSUserDefaults.StandardUserDefaults.StringForKey(GetFieldKey(id, NotificationEntryMessageGeofenceRegionKey)); string notificationExitMessage = NSUserDefaults.StandardUserDefaults.StringForKey(GetFieldKey(id, NotificationExitMessageGeofenceRegionKey)); string notificationStayMessage = NSUserDefaults.StandardUserDefaults.StringForKey(GetFieldKey(id, NotificationStayMessageGeofenceRegionKey)); bool showNotification = NSUserDefaults.StandardUserDefaults.BoolForKey(GetFieldKey(id, ShowNotificationGeofenceRegionKey)); bool persistent = NSUserDefaults.StandardUserDefaults.BoolForKey(GetFieldKey(id, PersistentGeofenceRegionKey)); bool showEntryNotification = NSUserDefaults.StandardUserDefaults.BoolForKey(GetFieldKey(id, ShowEntryNotificationGeofenceRegionKey)); bool showExitNotification = NSUserDefaults.StandardUserDefaults.BoolForKey(GetFieldKey(id, ShowExitNotificationGeofenceRegionKey)); bool showStayNotification = NSUserDefaults.StandardUserDefaults.BoolForKey(GetFieldKey(id, ShowStayNotificationGeofenceRegionKey)); int stayedInThresholdDuration = (int)NSUserDefaults.StandardUserDefaults.IntForKey(GetFieldKey(id, StayedInThresholdDurationGeofenceRegionKey)); region = new GeofenceCircularRegion(id,lat,lon,radius,notifyOnEntry,notifyOnExit,notifyOnStay,showNotification,persistent,showEntryNotification,showExitNotification,showStayNotification) { NotificationEntryMessage = notificationEntryMessage, NotificationStayMessage = notificationStayMessage, NotificationExitMessage = notificationExitMessage, StayedInThresholdDuration=TimeSpan.FromMilliseconds(stayedInThresholdDuration) }; } return region; }
public override void Save(GeofenceCircularRegion region) { string id = region.Id; if(string.IsNullOrEmpty(id)||!region.Persistent) return; NSUserDefaults.StandardUserDefaults.SetString(region.Id,GetFieldKey(id, IdGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetDouble(region.Latitude,GetFieldKey(id, LatitudeGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetDouble(region.Longitude,GetFieldKey(id, LongitudeGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetBool(region.NotifyOnEntry,GetFieldKey(id, NotifyOnEntryGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetBool(region.NotifyOnExit,GetFieldKey(id, NotifyOnExitGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetBool(region.NotifyOnStay,GetFieldKey(id, NotifyOnStayGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetDouble(region.Radius,GetFieldKey(id, RadiusGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetBool(region.ShowNotification, GetFieldKey(id, ShowNotificationGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetBool(region.Persistent, GetFieldKey(id, PersistentGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetBool(region.ShowEntryNotification, GetFieldKey(id, ShowEntryNotificationGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetBool(region.ShowExitNotification, GetFieldKey(id, ShowExitNotificationGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetBool(region.ShowStayNotification, GetFieldKey(id, ShowStayNotificationGeofenceRegionKey)); NSUserDefaults.StandardUserDefaults.SetInt((int)region.StayedInThresholdDuration.TotalMilliseconds, GetFieldKey(id, StayedInThresholdDurationGeofenceRegionKey)); if(!string.IsNullOrEmpty(region.NotificationEntryMessage)) { NSUserDefaults.StandardUserDefaults.SetString(region.NotificationEntryMessage, GetFieldKey(id, NotificationEntryMessageGeofenceRegionKey)); } if (!string.IsNullOrEmpty(region.NotificationExitMessage)) { NSUserDefaults.StandardUserDefaults.SetString(region.NotificationExitMessage, GetFieldKey(id, NotificationExitMessageGeofenceRegionKey)); } if(!string.IsNullOrEmpty(region.NotificationStayMessage)) { NSUserDefaults.StandardUserDefaults.SetString(region.NotificationStayMessage, GetFieldKey(id, NotificationStayMessageGeofenceRegionKey)); } geofenceIds.Add(id); NSUserDefaults.StandardUserDefaults.SetString(string.Join(IdSeparator,geofenceIds.ToArray<string>()),GeofenceIdsKey); NSUserDefaults.StandardUserDefaults.Synchronize(); }
/// <summary> /// Saves geofence region in store /// </summary> /// <param name="region"></param> public abstract void Save(GeofenceCircularRegion region);
void AddRegion(GeofenceCircularRegion region) { CLRegion cRegion = null; if (UIDevice.CurrentDevice.CheckSystemVersion(7, 0)) { cRegion = new CLCircularRegion(new CLLocationCoordinate2D(region.Latitude, region.Longitude), (region.Radius > locationManager.MaximumRegionMonitoringDistance) ? locationManager.MaximumRegionMonitoringDistance : region.Radius, region.Id); } else { cRegion = new CLRegion(new CLLocationCoordinate2D(region.Latitude, region.Longitude), (region.Radius > locationManager.MaximumRegionMonitoringDistance) ? locationManager.MaximumRegionMonitoringDistance : region.Radius, region.Id); } cRegion.NotifyOnEntry = region.NotifyOnEntry || region.NotifyOnStay; cRegion.NotifyOnExit = region.NotifyOnExit; locationManager.StartMonitoring(cRegion); locationManager.RequestState(cRegion); }
/// <summary> /// Starts monitoring region /// </summary> /// <param name="region"></param> public void StartMonitoring(GeofenceCircularRegion region) { if(AvailableForMonitoring()) { if (!mRegions.ContainsKey(region.Id)) { mRegions.Add(region.Id, region); }else{ mRegions[region.Id]=region; } GeofenceStore.SharedInstance.Save(region); if (Regions.Count > 20 && locationManager.MonitoredRegions.Count == 20) { RecalculateRegions(); } else { AddRegion(region); } locationManager.StartMonitoringSignificantLocationChanges(); } }
/// <summary> /// Saves geofence region in store /// </summary> /// <param name="region"></param> public abstract void Save(GeofenceCircularRegion region);
void AddRegion(GeofenceCircularRegion region) { CLRegion cRegion = null; if (UIDevice.CurrentDevice.CheckSystemVersion(7, 0)) { cRegion = new CLCircularRegion(new CLLocationCoordinate2D(region.Latitude, region.Longitude), (region.Radius > locationManager.MaximumRegionMonitoringDistance) ? locationManager.MaximumRegionMonitoringDistance : region.Radius, region.Id); } else { cRegion = new CLRegion(new CLLocationCoordinate2D(region.Latitude, region.Longitude), (region.Radius > locationManager.MaximumRegionMonitoringDistance) ? locationManager.MaximumRegionMonitoringDistance : region.Radius, region.Id); } cRegion.NotifyOnEntry = region.NotifyOnEntry || region.NotifyOnStay; cRegion.NotifyOnExit = region.NotifyOnExit; locationManager.StartMonitoring(cRegion); // Request state for this region, putting request behind a timer per thread: http://stackoverflow.com/questions/24543814/diddeterminestate-not-always-called Task.Run(async() => { await Task.Delay(TimeSpan.FromSeconds(2)); locationManager.RequestState(cRegion); }); }