Share via


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

這個主題說明如何使用 HTML5 中的 W3C 地理位置 API 來回應使用者地理位置的變更。

先決條件

您應該熟悉 HTML 和 JavaScript。

指示

步驟 1: 開啟 Microsoft Visual Studio

開啟 Visual Studio 的執行個體。

步驟 2: 驗證是否啟用定位功能

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

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

步驟 3: 建立新專案

建立新專案,從 [JavaScript/市集應用程式] 專案類型中選擇 [空白應用程式]****。

步驟 4: 啟用定位功能

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

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

步驟 5: 取代 JavaScript 程式碼

在共用專案中開啟 default.js (/js/default.js)。使用下列程式碼取代檔案中的程式碼。

(function () {
    "use strict";

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {

            args.setPromise(WinJS.UI.processAll().
                done(function () {

                    // Add an event handler to the button.
                    document.querySelector("#watchLoc").addEventListener("click",
                        watchloc);

                    // Add an event handler to the button.
                    document.querySelector("#stopWatching").addEventListener("click",
                        stopwatching);

                }));
        }
    };

    var loc = null;
    var watchId;

    function watchloc() {
        if (loc == null) {
            loc = window.navigator.geolocation;
        }
        if (loc != null) {
            watchId = loc.watchPosition(successCallback);
        }
    }

    function stopwatching() {
        loc.clearWatch(watchId);
    }

    function successCallback(pos) {
        document.getElementById('latitude').innerHTML = pos.coords.latitude;
        document.getElementById('longitude').innerHTML = pos.coords.longitude;
        document.getElementById('accuracy').innerHTML = pos.coords.accuracy;
    }

    function errorCallback(error) {
        var strMessage = "";

        // Check for known errors
        switch (error.code) {
            case error.PERMISSION_DENIED:
                strMessage = "Access to your location is turned off. " +
                    "Change your settings to turn it back on.";
                break;
            case error.POSITION_UNAVAILABLE:
                strMessage = "Data from location services is " +
                    "currently unavailable.";
                break;
            case error.TIMEOUT:
                strMessage = "Location could not be determined " +
                    "within a specified timeout period.";
                break;
            default:
                break;
        }
        document.getElementById("status").innerHTML = strMessage;
    }


    app.start();
})();

步驟 6: 新增應用程式的 HTML

開啟 Windows 和 Windows Phone 專案的 default.html 檔案,並將下列 HTML 複製到檔案的 BODY 標記中。

    Geolocation Event Sample<br />
    <br />

    <button id="watchLoc">Watch Location</button><br />
    <button id="stopWatching">Stop Watching</button><br />
    Latitude: <span id="latitude">Waiting for update...</span><br />
    Longitude: <span id="longitude">Waiting for update...</span><br />
    Accuracy: <span id="accuracy">Waiting for update...</span><br />
    <span id="status"> </span><br />

步驟 7: 建置應用程式

選擇 [建置]**** > [建置方案] 來建置專案。

步驟 8: 測試應用程式

  1. 在 [偵錯]**** 功能表中按一下 [開始偵錯] 來測試方案。
  2. 第一次執行範例時,您會看到一個提示,詢問應用程式是否可以使用您的位置。請選擇 [允許]**** 選項。
  3. 按一下 [取得定位] 按鈕以取得目前定位。

備註

定位服務使用一些不同的來源來判斷位置。如果無法使用 GPS、基地台及 Wi-Fi,它將會改用「IP 位址」。在此情況下,請注意,您可能無法取得任何位置更新事件,因為 IP 位址資料並不會經常更新。

相關主題

Windows 10 地理位置範例

Windows 8.1 地理位置範例