/// 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); }); } } } }
/* / Voor het toevoegen van icon's aan de map. */ private void AddMapIcon(LocationData location) { MapIcon MapIcon1 = new MapIcon(); MapIcon1.Location = new Geopoint(location.position); MapIcon1.NormalizedAnchorPoint = new Point(0.5, 1.0); MapIcon1.Title = location.name; InputMap.MapElements.Add(MapIcon1); // Geofence BasicGeoposition pos = new BasicGeoposition(); pos.Latitude = location.position.Latitude; pos.Longitude = location.position.Longitude; Geocircle circle = new Geocircle(pos, 35); MonitoredGeofenceStates monitoredStates = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited | MonitoredGeofenceStates.Removed; TimeSpan dwellTime = TimeSpan.FromSeconds(1); var geofence = new Windows.Devices.Geolocation.Geofencing.Geofence(location.name, circle, monitoredStates, false, dwellTime); GeofenceMonitor.Current.Geofences.Add(geofence); drawGeofence(location, 35); }