Beispiel #1
0
        /// <summary>
        /// Get called when Region is left
        /// </summary>
        /// <param name="region">Region thats left.</param>
        private void OnRegionLeft(CLRegion region)
        {
            if (!this.geofenceResults.ContainsKey(region.Identifier))
            {
                this.geofenceResults.Add(
                    region.Identifier,
                    new GeofenceResult
                {
                    RegionId = region.Identifier
                });
            }

            if (this.LastKnownLocation != null)
            {
                this.geofenceResults[region.Identifier].Latitude  = this.LastKnownLocation.Latitude;
                this.geofenceResults[region.Identifier].Longitude = this.LastKnownLocation.Longitude;
                this.geofenceResults[region.Identifier].Accuracy  = this.LastKnownLocation.Accuracy;
            }
            else
            {
                this.geofenceResults[region.Identifier].Latitude  = region.Center.Latitude;
                this.geofenceResults[region.Identifier].Longitude = region.Center.Longitude;
                this.geofenceResults[region.Identifier].Accuracy  = region.Radius;
            }

            this.geofenceResults[region.Identifier].LastExitTime = DateTime.Now;
            this.geofenceResults[region.Identifier].Transition   = GeofenceTransition.Exited;

            CrossGeofence.GeofenceListener.OnRegionStateChanged(this.geofenceResults[region.Identifier]);

            if (this.Regions[region.Identifier].ShowNotification)
            {
                GeofenceImplementation.CreateNotification(ViewAction, string.IsNullOrEmpty(this.Regions[region.Identifier].NotificationExitMessage) ? this.GeofenceResults[region.Identifier].ToString() : this.Regions[region.Identifier].NotificationExitMessage);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Get Called on RegionEntered.
        /// </summary>
        /// <param name="region">Region entered.</param>
        private async void OnRegionEntered(CLRegion region)
        {
            if (this.GeofenceResults.ContainsKey(region.Identifier) && this.GeofenceResults[region.Identifier].Transition == GeofenceTransition.Entered)
            {
                return;
            }

            if (!this.geofenceResults.ContainsKey(region.Identifier))
            {
                this.geofenceResults.Add(
                    region.Identifier,
                    new GeofenceResult
                {
                    RegionId = region.Identifier
                });
            }

            if (this.LastKnownLocation != null)
            {
                this.geofenceResults[region.Identifier].Latitude  = this.LastKnownLocation.Latitude;
                this.geofenceResults[region.Identifier].Longitude = this.LastKnownLocation.Longitude;
                this.geofenceResults[region.Identifier].Accuracy  = this.LastKnownLocation.Accuracy;
            }
            else
            {
                this.geofenceResults[region.Identifier].Latitude  = region.Center.Latitude;
                this.geofenceResults[region.Identifier].Longitude = region.Center.Longitude;
                this.geofenceResults[region.Identifier].Accuracy  = region.Radius;
            }

            this.geofenceResults[region.Identifier].LastEnterTime = DateTime.Now;
            this.geofenceResults[region.Identifier].LastExitTime  = null;
            this.geofenceResults[region.Identifier].Transition    = GeofenceTransition.Entered;

            if (region.NotifyOnEntry)
            {
                CrossGeofence.GeofenceListener.OnRegionStateChanged(this.geofenceResults[region.Identifier]);

                if (this.Regions.ContainsKey(region.Identifier) && this.Regions[region.Identifier].ShowNotification)
                {
                    GeofenceImplementation.CreateNotification(ViewAction, string.IsNullOrEmpty(this.Regions[region.Identifier].NotificationEntryMessage) ? this.GeofenceResults[region.Identifier].ToString() : this.Regions[region.Identifier].NotificationEntryMessage);
                }
            }

            // Checks if device has stayed asynchronously
            await GeofenceImplementation.CheckIfStayed(region.Identifier);
        }
Beispiel #3
0
        /// <summary>
        /// Checks if has passed to stayed state
        /// </summary>
        /// <returns>The if stayed.</returns>
        /// <param name="regionId">Region identifier.</param>
        public static async Task CheckIfStayed(string regionId)
        {
            if (CrossGeofence.Current.GeofenceResults.ContainsKey(regionId) && CrossGeofence.Current.Regions.ContainsKey(regionId) && CrossGeofence.Current.Regions[regionId].NotifyOnStay && CrossGeofence.Current.GeofenceResults[regionId].Transition == GeofenceTransition.Entered && Math.Abs(CrossGeofence.Current.Regions[regionId].StayedInThresholdDuration.TotalMilliseconds) > float.Epsilon)
            {
                await Task.Delay((int)CrossGeofence.Current.Regions[regionId].StayedInThresholdDuration.TotalMilliseconds);

                if (CrossGeofence.Current.GeofenceResults[regionId].LastExitTime == null && CrossGeofence.Current.GeofenceResults[regionId].Transition != GeofenceTransition.Stayed)
                {
                    CrossGeofence.Current.GeofenceResults[regionId].Transition = GeofenceTransition.Stayed;

                    CrossGeofence.GeofenceListener.OnRegionStateChanged(CrossGeofence.Current.GeofenceResults[regionId]);

                    if (CrossGeofence.Current.Regions[regionId].ShowNotification)
                    {
                        GeofenceImplementation.CreateNotification(ViewAction, string.IsNullOrEmpty(CrossGeofence.Current.Regions[regionId].NotificationStayMessage) ? CrossGeofence.Current.GeofenceResults[regionId].ToString() : CrossGeofence.Current.Regions[regionId].NotificationStayMessage);
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Get current 20 monitored regions.
        /// </summary>
        /// <param name="regions">List of all regions</param>
        /// <returns>current 20 monitored regions</returns>
        public IList <GeofenceCircularRegion> GetCurrentRegions(IList <GeofenceCircularRegion> regions)
        {
            IList <GeofenceCircularRegion> nearestRegions;

            if (regions.Count > 20)
            {
                if (this.locationManager.Location != null)
                {
                    IEnumerable <GeofenceCircularRegion> newRegions = regions.OrderBy(r => GeofenceImplementation.CalculateDistance(this.locationManager.Location.Coordinate.Latitude, this.locationManager.Location.Coordinate.Longitude, r.Latitude, r.Longitude)).Take(20);

                    nearestRegions = newRegions.ToList();
                }
                else
                {
                    nearestRegions = regions.Take(20).ToList();
                }
            }
            else
            {
                nearestRegions = regions;
            }

            return(nearestRegions);
        }