地理围栏定义围绕兴趣点的圆形地理边界。 当用户进入或退出边界时,应用会收到通知。 地理围栏适用于基于位置的提醒、警报、签到和情境内容分发。
本文介绍如何在Windows 应用 SDK(WinUI 3)应用中创建地理围栏、监视状态更改和处理地理围栏事件。
先决条件
- 从“空白应用,已打包(桌面版 WinUI 3)”模板创建的 WinUI 3 项目。
- 在程序包清单中声明的 位置 功能。 有关说明,请参阅 获取用户的位置。
请求位置访问
在创建或监视地理围栏之前调用 Geolocator.RequestAccessAsync 。
using Windows.Devices.Geolocation;
using Windows.Devices.Geolocation.Geofencing;
var accessStatus = await Geolocator.RequestAccessAsync();
if (accessStatus != GeolocationAccessStatus.Allowed)
{
StatusText.Text = "Location access is required for geofencing.";
return;
}
创建地理围栏
通过指定标识符、地理中心、以米为单位的半径以及要监控的状态转换(进入、退出或移除)来定义地理围栏。
var position = new BasicGeoposition
{
Latitude = 47.6062,
Longitude = -122.3321
};
var geocircle = new Geocircle(position, 200); // 200-meter radius
var geofence = new Geofence(
"SeattleDowntown", // Unique identifier
geocircle, // Geographic boundary
MonitoredGeofenceStates.Entered | // States to monitor
MonitoredGeofenceStates.Exited,
false, // Single use: false = persistent
TimeSpan.FromSeconds(10) // Dwell time before triggering
);
GeofenceMonitor.Current.Geofences.Add(geofence);
地理围栏参数
| 参数 | Description |
|---|---|
id |
标识地理围栏的唯一字符串。 使用此字符串区分来自不同地理围栏的事件。 |
geoshape |
定义边界的 地理围栏 。 仅支持圆形边界。 |
monitoredStates |
要监视哪些转换:Entered、Exited 或 Removed。 与 | 运算符结合使用。 |
singleUse |
如果 true,地理围栏将触发一次并自动移除。 |
dwellTime |
在事件触发前,用户必须在边界内(或外)停留多长时间。 有助于过滤掉短暂穿越。 |
在前台监视地理围栏事件
订阅 GeofenceMonitor.Current.GeofenceStateChanged 事件,以在应用运行时接收通知。
GeofenceMonitor.Current.GeofenceStateChanged += OnGeofenceStateChanged;
读取报表并更新 UI。 事件在后台线程上触发,因此请使用 DispatcherQueue 来封送 UI 更新。
private void OnGeofenceStateChanged(GeofenceMonitor sender, object args)
{
var reports = sender.ReadReports();
DispatcherQueue.TryEnqueue(() =>
{
foreach (var report in reports)
{
var state = report.NewState;
var id = report.Geofence.Id;
switch (state)
{
case GeofenceState.Entered:
StatusText.Text = $"Entered geofence: {id}";
break;
case GeofenceState.Exited:
StatusText.Text = $"Exited geofence: {id}";
break;
case GeofenceState.Removed:
StatusText.Text = $"Geofence removed: {id}";
// Re-add the geofence if it was removed due to expiration
break;
}
}
});
}
监视地理围栏状态更改
使用 GeofenceMonitor.Current.StatusChanged 检测地理围栏监视何时被禁用 — 例如,当用户关闭位置服务时。
GeofenceMonitor.Current.StatusChanged += (sender, args) =>
{
DispatcherQueue.TryEnqueue(() =>
{
var status = sender.Status;
if (status == GeofenceMonitorStatus.Disabled)
{
StatusText.Text = "Geofence monitoring is disabled. Check location settings.";
}
});
};
Tip
使用地理围栏时,通过 GeofenceMonitor.StatusChanged 而不是 Geolocator.StatusChanged 监视权限更改。 值为 Disabled 的 GeofenceMonitorStatus 等同于 Disabled 的 PositionStatus,但 GeofenceMonitorStatus 为地理围栏场景提供了更多上下文。
删除地理围栏
通过在 GeofenceMonitor.Current.Geofences 集合中查找来移除地理围栏。
var geofences = GeofenceMonitor.Current.Geofences;
var target = geofences.FirstOrDefault(g => g.Id == "SeattleDowntown");
if (target != null)
{
geofences.Remove(target);
}
最佳做法
- 设置合理的停留时间。 停留时间至少为 10 秒有助于筛选掉 GPS 抖动,并防止边界边缘出现虚假触发器。
- 使用至少 50 米的半径。 GPS 准确性因设备和环境而异。 小于 50 米的半径可能会产生不可靠的结果。
- 如果需要,请检查互联网连接。 如果应用在触发地理围栏事件(例如向服务器发送通知)时执行网络操作,请在创建地理围栏之前验证连接性。
- 处理
Removed状态。 如果地理围栏过期或系统面临资源压力,系统可能会移除它们。 请检查是否存在GeofenceState.Removed,如果您的场景需要,请重新创建地理围栏。 - 除非必要,请勿同时为同一地理围栏监视前台和后台。 如果这样做,在应用暂停时注销前台侦听器,并在应用恢复时重新注册。