GeofenceMonitor Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
public ref class GeofenceMonitor sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class GeofenceMonitor final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class GeofenceMonitor
Public NotInheritable Class GeofenceMonitor
- Inheritance
- Attributes
Windows requirements
| Requirements | Description |
|---|---|
| Device family |
Windows 10 (introduced in 10.0.10240.0)
|
| API contract |
Windows.Foundation.UniversalApiContract (introduced in v1.0)
|
| App capabilities |
location
|
Examples
Processing geofence state changes in a background task
public sealed class GeofenceBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral();
try
{
ProcessGeofenceStateChangedReports();
}
catch (UnauthorizedAccessException)
{
// Location permissions disabled - handle gracefully
// App-specific: log error and notify user when app resumes
}
finally
{
deferral.Complete();
}
}
private void ProcessGeofenceStateChangedReports()
{
var monitor = GeofenceMonitor.Current;
var reports = monitor.ReadReports();
foreach (var report in reports)
{
var geofenceId = report.Geofence.Id;
var timestamp = report.Geoposition.Coordinate.Timestamp;
// Handle different state changes
switch (report.NewState)
{
case GeofenceState.Entered:
// App-specific: log entry, trigger notifications, start tracking
HandleGeofenceEntered(geofenceId, timestamp);
break;
case GeofenceState.Exited:
// App-specific: log exit, stop location tracking, update analytics
HandleGeofenceExited(geofenceId, timestamp);
break;
case GeofenceState.Removed:
// Handle automatic removal (expired or single-use)
var reason = report.RemovalReason;
if (reason == GeofenceRemovalReason.Expired)
{
// App-specific: re-register if needed, update UI
HandleGeofenceExpired(geofenceId);
}
else if (reason == GeofenceRemovalReason.Used)
{
// App-specific: process single-use completion
HandleGeofenceUsed(geofenceId);
}
break;
}
}
// App-specific: persist state changes, update tile notifications
if (reports.Count > 0)
{
SaveReportSummary(reports);
}
}
}
Registering geofences and handling events in foreground
winrt::fire_and_forget SetupGeofenceMonitoring()
{
using namespace winrt::Windows::Devices::Geolocation;
using namespace winrt::Windows::Devices::Geolocation::Geofencing;
auto monitor = GeofenceMonitor::Current();
// Check permission before proceeding
auto accessStatus = co_await Geolocator::RequestAccessAsync();
if (accessStatus != GeolocationAccessStatus::Allowed)
{
// App-specific: show permission dialog or disable geofencing features
co_return;
}
// Register for state change events with C++/WinRT event handling
monitor.GeofenceStateChanged({ this, &MainPage::OnGeofenceStateChanged });
monitor.StatusChanged({ this, &MainPage::OnGeofenceMonitorStatusChanged });
// Create and register a geofence
BasicGeoposition position{};
position.Latitude = 47.6062;
position.Longitude = -122.3321;
auto geocircle = Geocircle(position, 100.0); // 100 meter radius
auto geofence = Geofence(L"home-location", geocircle);
monitor.Geofences().Append(geofence);
}
void OnGeofenceStateChanged(GeofenceMonitor const& sender,
winrt::Windows::Foundation::IInspectable const&)
{
// Process real-time state changes (app is running)
auto reports = sender.ReadReports();
for (auto const& report : reports)
{
// App-specific: update UI, show notifications
UpdateLocationStatus(report.Geofence().Id(), report.NewState());
}
}
Remarks
Use the static Current property to get the GeofenceMonitor object which contains all of an app's geofence information.
Key properties and methods
- Geofences: Collection to register and manage geofences for monitoring
- ReadReports: Retrieves state change reports since the last read
- GeofenceStateChanged: Event fired when geofence states change
- Status: Current operational status of the monitor
Background task integration
The GeofenceMonitor class is commonly used in background tasks to handle geofence events when the app is not running. Background tasks should call ReadReports to process accumulated state changes and clear the report queue.
Important
Applications must handle UnauthorizedAccessException when accessing GeofenceMonitor if location permissions are disabled. Check location access before registering geofences or reading reports.
Properties
| Name | Description |
|---|---|
| Current |
Gets the GeofenceMonitor object which contains all of an app's Geofence information. |
| Geofences |
Returns a vector of the app's Geofence objects currently registered with the system-wide GeofenceMonitor. |
| LastKnownGeoposition |
Last reading of the device's location. |
| Status |
Indicates the current state of the GeofenceMonitor. |
Methods
| Name | Description |
|---|---|
| ReadReports() |
Gets a collection of status changes to the Geofence objects in the Geofences collection of the GeofenceMonitor. |
Events
| Name | Description |
|---|---|
| GeofenceStateChanged |
Raised when the state of one or more Geofence objects in the Geofences collection of the GeofenceMonitor has changed. |
| StatusChanged |
Raised when the status of the GeofenceMonitor has changed. |