Share via


設定地理柵欄 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

本主題將逐步引導您完成在應用程式中設定 Geofence 的步驟。

藍圖: 這個主題與其他主題的相關性?請參閱:

簡介

設定 Geofence 時,有一些要採取的步驟。 除了定義相關區域之外,您也必須確認您擁有適當的位置權限。 最後,您必須設定一個事件處理常式,以防使用者在應用程式執行時變更那些權限。

確認是否已啟用位置

必須先在裝置上啟用 [位置],您的應用程式才能存取位置。在 [設定]**** 應用程式中,確認已開啟下列位置隱私權設定

  • 已將 [此裝置的位置...] 設為 [開啟]**** (不適用於 Windows 10 行動裝置版)
  • 已將定位服務設定的 [位置] 設為 [開啟]****
  • 在 [選擇可以使用您的位置的應用程式] 底下,將您的應用程式設為 [開啟]****

啟用定位功能

在 [方案總管] 中按兩下 package.appxmanifest,然後選取 [功能] 索引標籤。接著,核取 [功能]**** 清單中的 [位置]。這會將 Location 裝置功能新增至套件資訊清單檔案中。

  <Capabilities>
    <!-- DeviceCapability elements must follow Capability elements (if present) -->
    <DeviceCapability Name="location"/>
  </Capabilities>

檢查位置權限

首先,您要在初始化期間將程式碼新增到應用程式,以取得位置。 在 Windows 上,您的應用程式第一次使用 API 取得目前位置時,系統會提示使用者提供位置權限。 如果應用程式沒有來自使用者的權限,便會警示使用者。 請注意,您仍然可以在沒有位置權限的情況下設定 Geofence,但您在尚未啟用權限之前,將不會收到任何通知。

    function initialize() {

        promise = geolocator.getGeopositionAsync();
        promise.done(
            function (pos) {
                var coord = pos.coordinate;

            },
            function (err) {
            // handle situations where location permissions are not granted to your app
            }
        );

    }


接聽位置權限變更

接著,您要確認是否登錄權限變更事件,以免使用者因為特定原因而決定關閉位置權限。 首先,將事件處理常式新增到初始化方法:


var accessInfo = null;
accessInfo = DeviceAccessInformation.createFromDeviceClass(Enumeration.DeviceClass.location);
accessInfo.addEventListener("accesschanged", onAccessChanged);

接著,處理權限變更,讓使用者知道地理柵欄在關閉位置權限後,將無法再運作:

function onAccessChanged(args) {
    var eventDescription = getTimeStampedMessage("Device Access Status");
    var item = null;

    if (DeviceAccessStatus.deniedByUser === args.status) {
        eventDescription += " (DeniedByUser)";

        WinJS.log && WinJS.log("Location has been disabled by the user. Enable access through the settings charm.", "sample", "status");
    } else if (DeviceAccessStatus.deniedBySystem === args.status) {
        eventDescription += " (DeniedBySystem)";

        WinJS.log && WinJS.log("Location has been disabled by the system. The administrator of the device must enable location access through the location control panel.", "sample", "status");
    } else if (DeviceAccessStatus.unspecified === args.status) {
        eventDescription += " (Unspecified)";

        WinJS.log && WinJS.log("Location has been disabled by unspecified source. The administrator of the device may need to enable location access through the location control panel, then enable access through the settings charm.", "sample", "status");
    } else if (DeviceAccessStatus.allowed === args.status) {
        eventDescription += " (Allowed)";

        // clear status
        WinJS.log && WinJS.log("", "sample", "status");
    } else {
        eventDescription += " (Unknown)";

        WinJS.log && WinJS.log("Unknown device access information status", "sample", "status");
    }

    addEventDescription(eventDescription);
}

注意  您可以藉由檢查 LocationStatus 屬性,來判斷使用者是否已在 [設定] 中停用位置。如果這個值是 Disabled,則表示已停用位置。

 

建立地理柵欄

現在您已經準備好,可以定義並設定地理柵欄。 可以針對地理柵欄設定的一些值包括:

  • 用於識別地理柵欄的 Id
  • Geoshape 定義的相關圓形區域。
  • MonitoredStates,表示您希望收到通知的地理柵欄事件:進入已定義的區域、離開已定義的區域,或移除地理柵欄。
  • SingleUse 旗標,一旦符合所監控之地理柵欄的所有狀態之後,將會移除該地理柵欄。
  • DwellTime,表示在觸發進入/離開事件之前,使用者必須處於已定義之區域內部或外部的時間長度。
  • StartTime,表示開始監控地理柵欄的時間。
  • 要監控其地理柵欄的 Duration

function generateGeofence() {
    var geofence = null;

    try {
        var fenceKey = nameElement.value;

        var position = {
            latitude: decimalFormatter.parseDouble(latitude.value),
            longitude: decimalFormatter.parseDouble(longitude.value),
            altitude: 0
        };
        var radiusValue = decimalFormatter.parseDouble(radius.value);

        // the geofence is a circular region
        var geocircle = new Windows.Devices.Geolocation.Geocircle(position, radiusValue);

        var singleUse = false;

        if (geofenceSingleUse.checked) {
            singleUse = true;
        }

        // want to listen for enter geofence, exit geofence and remove geofence events
        var mask = 0;

        mask = mask | Windows.Devices.Geolocation.Geofencing.MonitoredGeofenceStates.entered;
        mask = mask | Windows.Devices.Geolocation.Geofencing.MonitoredGeofenceStates.exited;
        mask = mask | Windows.Devices.Geolocation.Geofencing.MonitoredGeofenceStates.removed;

        var dwellTimeSpan = new Number(parseTimeSpan(dwellTimeField, defaultDwellTimeSeconds));
        var durationTimeSpan = null;
        if (durationField.value.length) {
            durationTimeSpan = new Number(parseTimeSpan(durationField, 0));
        } else {
            durationTimeSpan = new Number(0); // duration needs to be set since start time is set below
        }
        var startDateTime = null;
        if (startTimeField.value.length) {
            startDateTime = new Date(startTimeField.value);
        } else {
            startDateTime = new Date(); // if you don't set start time in JavaScript the start time defaults to 1/1/1601
        }

        geofence = new Windows.Devices.Geolocation.Geofencing.Geofence(fenceKey, geocircle, mask, singleUse, dwellTimeSpan, startDateTime, durationTimeSpan);
    } catch (ex) {
        WinJS.log && WinJS.log(ex.toString(), "sample", "error");
    }

    return geofence;
}

相關主題

藍圖

使用 JavaScript 建立應用程式的藍圖

設計應用程式的 UX

工作

在前景處理地理柵欄通知

在背景接聽地理柵欄事件

從背景工作處理地理柵欄通知

參考

Geoshape

Geofence

Geolocator

其他資源

Windows 10 地理位置範例

Windows 8.1 地理位置範例

地理柵欄的指導方針