共用方式為


地理柵欄 App 的指導方針

注意

MapControl 和地圖服務要求地圖驗證金鑰,稱為 MapServiceToken。 如需取得和設定地圖驗證金鑰的詳細資訊,請參閱要求地圖驗證金鑰

重要 API

請遵循應用程式中地理柵欄的這些最佳做法。

建議

  • 如果您的應用程式在發生 Geofence 事件時需要存取網際網路,請先檢查網際網路存取權限,再建立地理柵欄。

    • 如果應用程式目前沒有網際網路存取權,您可以先提示使用者連線到網際網路,再設定地理柵欄。
    • 如果無法存取網際網路,請避免耗用地理柵欄位置檢查所需的電源。
  • 當地理柵欄事件指示 EnteredExited 狀態發生變化時,透過檢查時間戳記和目前位置來確保地理柵欄通知的相關性。 如需詳細資訊,請參閱下方的檢查時間戳記和目前位置。 如需詳細資訊,請參閱下方的 (#timestamp)。

  • 建立例外狀況來管理裝置無法存取位置資訊的情況,並在必要時通知使用者。 定位資訊可能無法使用,原因是權限被關閉、裝置沒有 GPS 收發器、GPS 訊號被封鎖,或 Wi-Fi 訊號不夠強。

  • 一般而言,不需要同時接聽前景和背景中的地理柵欄事件。 不過,如果您的應用程式需要接聽前景和背景中的地理柵欄事件:

    • 呼叫 ReadReports 方法,以找出是否已發生事件。
    • 在使用者看不到您的應用程式時取消註冊前景事件接聽程式,並在應用程式再次顯示時重新註冊。

    如需程式碼範例和詳細資訊,請參閱背景和前景接聽程式

  • 每個應用程式不要使用超過 1000 個地理柵欄。 系統實際上支援每個應用程式使用數千個地理柵欄,使用不超過 1000 地理柵欄來減少應用程式的記憶體使用量,以維護良好的應用程式效能。

  • 請勿建立半徑小於 50 公尺的地理柵欄。 如果您的應用程式需要使用半徑較小的地理柵欄,建議使用者在裝置上使用具有 GPS 收發器的應用程式,以確保最佳效能。

其他用法指導方針

檢查時間戳記和目前位置

當事件指示 EnteredExited 狀態發生變化時,請檢查事件的時間戳記和目前位置。 各種因素,例如系統沒有足夠的資源來啟動背景工作、使用者沒有注意到通知,或者裝置處於待機狀態 (在 Windows 上),可能會影響事件實際由使用者處理的時間。 例如,可能會發生下列一系列事件:

  • 您的應用程式會建立地理柵欄,並監視進入和離開事件的地理柵欄。
  • 使用者在地理柵欄內移動裝置,導致觸發 enter 事件。
  • 您的應用程式會將通知傳送給使用者,指出他們現在位於地理柵欄內。
  • 使用者忙碌中,直到 10 分鐘後才注意到通知。
  • 在該 10 分鐘的延遲期間,使用者已移回地理柵欄之外。

從時間戳記中,您可以判斷過去發生的動作。 從目前的位置,您可以看到使用者現在回到地理柵欄之外。 視應用程式的功能而定,您可能會想要篩選掉此事件。

背景和前景接聽程式

一般而言,您的應用程式不需要同時在前景和背景工作中接聽 Geofence 事件。 處理可能需要兩者的情況最乾淨的方法是讓背景工作處理通知。 如果您同時設定前景和背景地理柵欄接聽程式,則無法保證會先觸發,因此您必須一律呼叫 ReadReports 方法來找出是否發生事件。

如果您同時設定前景和背景地理柵欄接聽程式,則每當使用者看不到您的應用程式,並在應用程式再次顯示時重新註冊您的應用程式時,您應該取消註冊前景事件接聽程式。 以下是註冊可見度事件的一些範例程式碼。

    Windows.UI.Core.CoreWindow coreWindow;    

    // This needs to be set before InitializeComponent sets up event registration for app visibility
    coreWindow = CoreWindow.GetForCurrentThread();
    coreWindow.VisibilityChanged += OnVisibilityChanged;
 document.addEventListener("visibilitychange", onVisibilityChanged, false);

當可見度變更時,您可以啟用或停用前景事件處理常式,如下所示。

private void OnVisibilityChanged(CoreWindow sender, VisibilityChangedEventArgs args)
{
    // NOTE: After the app is no longer visible on the screen and before the app is suspended
    // you might want your app to use toast notification for any geofence activity.
    // By registering for VisibiltyChanged the app is notified when the app is no longer visible in the foreground.

    if (args.Visible)
    {
        // register for foreground events
        GeofenceMonitor.Current.GeofenceStateChanged += OnGeofenceStateChanged;
        GeofenceMonitor.Current.StatusChanged += OnGeofenceStatusChanged;
    }
    else
    {
        // unregister foreground events (let background capture events)
        GeofenceMonitor.Current.GeofenceStateChanged -= OnGeofenceStateChanged;
        GeofenceMonitor.Current.StatusChanged -= OnGeofenceStatusChanged;
    }
}
function onVisibilityChanged() {
    // NOTE: After the app is no longer visible on the screen and before the app is suspended
    // you might want your app to use toast notification for any geofence activity.
    // By registering for VisibiltyChanged the app is notified when the app is no longer visible in the foreground.

    if (document.msVisibilityState === "visible") {
        // register for foreground events
        Windows.Devices.Geolocation.Geofencing.GeofenceMonitor.current.addEventListener("geofencestatechanged", onGeofenceStateChanged);
        Windows.Devices.Geolocation.Geofencing.GeofenceMonitor.current.addEventListener("statuschanged", onGeofenceStatusChanged);
    } else {
        // unregister foreground events (let background capture events)
        Windows.Devices.Geolocation.Geofencing.GeofenceMonitor.current.removeEventListener("geofencestatechanged", onGeofenceStateChanged);
        Windows.Devices.Geolocation.Geofencing.GeofenceMonitor.current.removeEventListener("statuschanged", onGeofenceStatusChanged);
    }
}

調整地理柵欄的大小

雖然 GPS 可以提供最精確的位置資訊,但地理柵欄也可以使用 Wi-Fi 或其他位置感應器來判斷使用者的目前位置。 但是使用這些其他方法可能會影響您可以建立的地理柵欄大小。 如果精確度很低,則建立小型地理柵欄並無用處。 一般而言,建議您不要建立半徑小於 50 公尺的地理柵欄。 此外,地理柵欄背景工作只會在 Windows 上定期執行;如果您使用小型地理柵欄,可能會完全錯過 EnterExit 事件。

如果您的應用程式需要使用半徑較小的地理柵欄,建議使用者在裝置上使用具有 GPS 收發器的應用程式,以確保最佳效能。