ジオフェンスは、目的地を中心とする円形の地理的境界を定義します。 ユーザーが境界に出入りすると、アプリは通知を受け取ります。 ジオフェンスは、場所ベースのリマインダー、アラート、チェックイン、コンテキスト コンテンツ配信に役立ちます。
この記事では、Windows アプリ SDK (WinUI 3) アプリでジオフェンスを作成し、状態の変化を監視し、ジオフェンス イベントを処理する方法について説明します。
前提条件
- Blank App, Packaged (WinUI 3 in Desktop)テンプレートから作成された WinUI 3 プロジェクト。
- パッケージ マニフェストで宣言されている Location 機能。 手順については 、「ユーザーの場所を取得 する」を参照してください。
場所へのアクセスを要求する
ジオフェンスを作成または監視する前に 、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);
ジオフェンス パラメーター
| Parameter | Description |
|---|---|
id |
ジオフェンスを識別する一意の文字列。 これを使用して、さまざまなジオフェンスからイベントを区別します。 |
geoshape |
境界を定義する Geocircle 。 循環境界のみがサポートされています。 |
monitoredStates |
監視する遷移: Entered、Exited、または Removed。
|演算子と組み合わせます。 |
singleUse |
true場合、ジオフェンスは 1 回トリガーされ、自動的に削除されます。 |
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
ジオフェンスを使用する場合は、 Geolocator.StatusChanged ではなく、GeofenceMonitor.StatusChanged を使用してアクセス許可 の変更を監視します。
Disabled 値は、PositionStatusのDisabledと同じですが、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を確認し、必要な場合はジオフェンスを再作成します。 - 必要な場合を除き、同じジオフェンスに対してフォアグラウンドとバックグラウンドを同時に監視しないでください。 その場合は、アプリが中断されたときにフォアグラウンド リスナーの登録を解除し、再開時に再登録します。
関連資料
Windows developer