private string GenerateReportDescription(GeofenceStateChangeReport report) { GeofenceState state = report.NewState; DateTimeOffset timestamp = report.Geoposition.Coordinate.Timestamp; string result = $"{report.Geofence.Id} {timestamp:G}"; if (state == GeofenceState.Removed) { GeofenceRemovalReason reason = report.RemovalReason; if (reason == GeofenceRemovalReason.Expired) { result += " (Removed/Expired)"; } else if (reason == GeofenceRemovalReason.Used) { result += " (Removed/Used)"; } } else if (state == GeofenceState.Entered) { result += " (Entered)"; } else if (state == GeofenceState.Exited) { result += " (Exited)"; } return(result); }
private void GetGeofenceStateChangedReports() { GeofenceMonitor monitor = GeofenceMonitor.Current; Geoposition posLastKnown = monitor.LastKnownGeoposition; string geofenceItemEvent = null; // Retrieve a vector of state change reports var reports = GeofenceMonitor.Current.ReadReports(); foreach (var report in reports) { GeofenceState state = report.NewState; geofenceItemEvent = report.Geofence.Id; if (state == GeofenceState.Removed) { GeofenceRemovalReason reason = report.RemovalReason; if (reason == GeofenceRemovalReason.Expired) { geofenceItemEvent += " (Removed/Expired)"; } else if (reason == GeofenceRemovalReason.Used) { geofenceItemEvent += " (Removed/Used)"; } } else if (state == GeofenceState.Entered) { geofenceItemEvent += " (Entered)"; } else if (state == GeofenceState.Exited) { geofenceItemEvent += " (Exited)"; } } // NOTE: Other notification mechanisms can be used here, such as Badge and/or Tile updates. DoToast(geofenceItemEvent); }
private void GetGeofenceStateChangedReports(Geoposition pos) { _geofenceBackgroundEvents.Clear(); FillEventCollectionWithExistingEvents(); GeofenceMonitor monitor = GeofenceMonitor.Current; Geoposition posLastKnown = monitor.LastKnownGeoposition; Windows.Globalization.Calendar calendar = new Windows.Globalization.Calendar(); Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatterLongTime; formatterLongTime = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("{hour.integer}:{minute.integer(2)}:{second.integer(2)}", new[] { "en-US" }, "US", Windows.Globalization.CalendarIdentifiers.Gregorian, Windows.Globalization.ClockIdentifiers.TwentyFourHour); bool eventOfInterest = true; // NOTE TO DEVELOPER: // Registered geofence events can be filtered out if the // geofence event time is stale. DateTimeOffset eventDateTime = pos.Coordinate.Timestamp; calendar.SetToNow(); DateTimeOffset nowDateTime = calendar.GetDateTime(); TimeSpan diffTimeSpan = nowDateTime - eventDateTime; long deltaInSeconds = diffTimeSpan.Ticks / oneHundredNanosecondsPerSecond; if (true == eventOfInterest) { if ((posLastKnown.Coordinate.Point.Position.Latitude != pos.Coordinate.Point.Position.Latitude) || (posLastKnown.Coordinate.Point.Position.Longitude != pos.Coordinate.Point.Position.Longitude)) { UpdateInforamtion.WipeGeolocDataFromAppData(); } if (true == eventOfInterest) { string geofenceItemEvent = null; int numEventsOfInterest = 0; // Retrieve a vector of state change reports var reports = GeofenceMonitor.Current.ReadReports(); foreach (var report in reports) { GeofenceState state = report.NewState; geofenceItemEvent = report.Geofence.Id + " " + formatterLongTime.Format(eventDateTime); if (state == GeofenceState.Removed) { GeofenceRemovalReason reason = report.RemovalReason; if (reason == GeofenceRemovalReason.Expired) { geofenceItemEvent += " (Removed/Expired)"; } else if (reason == GeofenceRemovalReason.Used) { geofenceItemEvent += " (Removed/Used)"; } } else if (state == GeofenceState.Entered) { geofenceItemEvent += " (Entered)"; } else if (state == GeofenceState.Exited) { geofenceItemEvent += " (Exited)"; } AddGeofenceEvent(geofenceItemEvent); ++numEventsOfInterest; } if (true == eventOfInterest && 0 != numEventsOfInterest) { SaveExistingEvents(); // NOTE: Other notification mechanisms can be used here, such as Badge and/or Tile updates. DoToast(numEventsOfInterest, geofenceItemEvent); } } } }
private void GetGeofenceStateChangedReports(Geoposition pos) { geofenceBackgroundEvents.Clear(); FillEventCollectionWithExistingEvents(); GeofenceMonitor monitor = GeofenceMonitor.Current; Geoposition posLastKnown = monitor.LastKnownGeoposition; Windows.Globalization.Calendar calendar = new Windows.Globalization.Calendar(); Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatterLongTime; formatterLongTime = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("{hour.integer}:{minute.integer(2)}:{second.integer(2)}", new[] { "en-US" }, "US", Windows.Globalization.CalendarIdentifiers.Gregorian, Windows.Globalization.ClockIdentifiers.TwentyFourHour); bool eventOfInterest = true; // NOTE TO DEVELOPER: // These events can be filtered out if the // geofence event time is stale. DateTimeOffset eventDateTime = pos.Coordinate.Timestamp; calendar.SetToNow(); DateTimeOffset nowDateTime = calendar.GetDateTime(); TimeSpan diffTimeSpan = nowDateTime - eventDateTime; long deltaInSeconds = diffTimeSpan.Ticks / oneHundredNanosecondsPerSecond; // NOTE TO DEVELOPER: // If the time difference between the geofence event and now is too large // the eventOfInterest should be set to false. if (eventOfInterest) { // NOTE TO DEVELOPER: // This event can be filtered out if the // geofence event location is too far away. if ((posLastKnown.Coordinate.Point.Position.Latitude != pos.Coordinate.Point.Position.Latitude) || (posLastKnown.Coordinate.Point.Position.Longitude != pos.Coordinate.Point.Position.Longitude)) { // NOTE TO DEVELOPER: // Use an algorithm like the Haversine formula or Vincenty's formulae to determine // the distance between the current location (pos.Coordinate) // and the location of the geofence event (latitudeEvent/longitudeEvent). // If too far apart set eventOfInterest to false to // filter the event out. } if (eventOfInterest) { string geofenceItemEvent = null; int numEventsOfInterest = 0; // Retrieve a vector of state change reports var reports = GeofenceMonitor.Current.ReadReports(); foreach (GeofenceStateChangeReport report in reports) { GeofenceState state = report.NewState; geofenceItemEvent = report.Geofence.Id + " " + formatterLongTime.Format(eventDateTime); if (state == GeofenceState.Removed) { GeofenceRemovalReason reason = report.RemovalReason; if (reason == GeofenceRemovalReason.Expired) { geofenceItemEvent += " (Removed/Expired)"; } else if (reason == GeofenceRemovalReason.Used) { geofenceItemEvent += " (Removed/Used)"; } } else if (state == GeofenceState.Entered) { geofenceItemEvent += " (Entered)"; } else if (state == GeofenceState.Exited) { geofenceItemEvent += " (Exited)"; } AddGeofenceEvent(geofenceItemEvent); ++numEventsOfInterest; } if (eventOfInterest && (0 != numEventsOfInterest)) { SaveExistingEvents(); // NOTE: Other notification mechanisms can be used here, such as Badge and/or Tile updates. DoToast(numEventsOfInterest, geofenceItemEvent); } } } }
private async Task GetGeofenceStateChangedReports(Geoposition pos) { _geofenceBackgroundEvents.Clear(); FillEventCollectionWithExistingEvents(); GeofenceMonitor monitor = GeofenceMonitor.Current; Geoposition posLastKnown = monitor.LastKnownGeoposition; Windows.Globalization.Calendar calendar = new Windows.Globalization.Calendar(); Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatterLongTime; formatterLongTime = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("{hour.integer}:{minute.integer(2)}:{second.integer(2)}", new[] { "hr-HR" }, "HR", Windows.Globalization.CalendarIdentifiers.Gregorian, Windows.Globalization.ClockIdentifiers.TwentyFourHour); bool eventOfInterest = true; // NOTE TO DEVELOPER: // Registered geofence events can be filtered out if the // geofence event time is stale. var eventDateTime = pos.Coordinate.Timestamp; calendar.SetToNow(); var nowDateTime = calendar.GetDateTime(); var diffTimeSpan = nowDateTime - eventDateTime; var deltaInSeconds = diffTimeSpan.TotalSeconds; if (deltaInSeconds > 120) { eventOfInterest = false; } if (eventOfInterest) { // NOTE TO DEVELOPER: // This event can be filtered out if the // geofence event location is too far away. var distance = 0.0d; if ((posLastKnown.Coordinate.Point.Position.Latitude != pos.Coordinate.Point.Position.Latitude) || (posLastKnown.Coordinate.Point.Position.Longitude != pos.Coordinate.Point.Position.Longitude)) { distance = new HaversineFormula().Distance(posLastKnown.Coordinate.Point.Position, pos.Coordinate.Point.Position, HaversineFormula.DistanceType.Kilometers); // NOTE TO DEVELOPER: // Use an algorithm like the Haversine formula or Vincenty's formulae to determine // the distance between the current location (pos.Coordinate) // and the location of the geofence event (latitudeEvent/longitudeEvent). // If too far apart set eventOfInterest to false to // filter the event out. } if (eventOfInterest) { string geofenceItemEvent = null; int numEventsOfInterest = 0; // Retrieve a vector of state change reports var reports = GeofenceMonitor.Current.ReadReports(); foreach (var report in reports) { GeofenceState state = report.NewState; geofenceItemEvent = report.Geofence.Id + " " + formatterLongTime.Format(eventDateTime); if (state == GeofenceState.Removed) { GeofenceRemovalReason reason = report.RemovalReason; if (reason == GeofenceRemovalReason.Expired) { geofenceItemEvent += " (Removed/Expired)"; } else if (reason == GeofenceRemovalReason.Used) { geofenceItemEvent += " (Removed/Used)"; } } else if (state == GeofenceState.Entered) { geofenceItemEvent += " (Entered)"; } else if (state == GeofenceState.Exited) { geofenceItemEvent += " (Exited)"; } geofenceItemEvent += $" - {deltaInSeconds}"; geofenceItemEvent += $"\nDistance - {distance}"; AddGeofenceEvent(geofenceItemEvent); ++numEventsOfInterest; var userLocation = new UserLocation() { Latitude = report.Geoposition.Coordinate.Point.Position.Latitude, Longitude = report.Geoposition.Coordinate.Point.Position.Longitude, Name = report.Geofence.Id, UpdatedUtc = eventDateTime.UtcDateTime, Status = (LocationStatus)(int)state }; await UploadDataToCloud(userLocation); } if (eventOfInterest == true && numEventsOfInterest != 0) { SaveExistingEvents(); // NOTE: Other notification mechanisms can be used here, such as Badge and/or Tile updates. //DoToast(numEventsOfInterest, geofenceItemEvent); } } } }
/// STEP 2B: On geofence notification, read reports /// Event handler for geofence state changes. /// </summary> /// <param name="sender">GeofenceMonitor, which receives the event notifications.</param> /// <param name="e">Event data that describes how this page was reached. The Parameter /// property is typically used to configure the page.</param> public async void OnGeofenceStateChangedHandler(GeofenceMonitor sender, object e) { // Get reports for geofences state changes var reports = sender.ReadReports(); foreach (GeofenceStateChangeReport report in reports) { GeofenceState state = report.NewState; Windows.Devices.Geolocation.Geofencing.Geofence geofence = report.Geofence; var gasStationItem = await SampleDataSource.GetItemAsync(geofence.Id); // If we've entered a particular geofence, show message for that geofence's id if (state == GeofenceState.Entered) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Show message for the given geofence enter event string geofenceEnterMessage = "Approaching " + gasStationItem.Subtitle; // Post message update Debug.WriteLine(geofenceEnterMessage); // Update Fence updateGasStationFence(report.Geofence.Id, (Geocircle)report.Geofence.Geoshape, Colors.Green); // Or create a toast to tell the user about the event SendToast(geofenceEnterMessage); // Or do stuff under the covers, such as update info, change something in the phone, send message, etc }); } // If we've exited a particular geofence, show message for that geofence's id else if (state == GeofenceState.Exited) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Show message for the given geofence exit event string geofenceExitMessage = "Departing " + gasStationItem.Subtitle; // Post message update Debug.WriteLine(geofenceExitMessage); // Draw Fence updateGasStationFence(report.Geofence.Id, (Geocircle)report.Geofence.Geoshape, Colors.Red); // Or create a toast to tell the user about the event //SendToast(geofenceExitMessage); // Or do stuff under the covers, such as check out, change something in the phone, send message, etc }); } else if (state == GeofenceState.Removed) { GeofenceRemovalReason reason = report.RemovalReason; string eventDescription = "Geofence Removed: "; if (reason == GeofenceRemovalReason.Expired) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { eventDescription += " (Removed/Expired)"; Debug.WriteLine(eventDescription + geofence.Id); }); } else if (reason == GeofenceRemovalReason.Used) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { eventDescription += " (Removed/Used)"; Debug.WriteLine(eventDescription + geofence.Id); }); } } } }