此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in English Always switch to English

Geolocation

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Geolocation 介面代表一個能夠以程式設計方式取得裝置位置的物件。它讓 Web 內容可以存取裝置的位置,這使得網站或應用程式能根據使用者的位置提供客製化的結果。

你可以透過由 Navigator 物件實作的 navigator.geolocation 屬性來取得此介面的物件。

備註: 基於安全考量,當網頁嘗試存取位置訊息時,會通知使用者並請求其授予權限。請注意,每個瀏覽器都有自己的策略和方法來請求此權限。

實體屬性

Geolocation 介面本身未實作、也未繼承任何屬性。

實體方法

Geolocation 介面未繼承任何方法。

Geolocation.getCurrentPosition()

判斷裝置的目前位置,並回傳一個包含資料的 GeolocationPosition 物件。

Geolocation.watchPosition()

回傳一個 long 值,此值代表一個新建立的回呼函式,該函式會在裝置位置改變時被調用。

Geolocation.clearWatch()

移除先前使用 watchPosition() 所安裝的特定處理函式。

規範

Specification
Geolocation
# geolocation_interface

瀏覽器相容性

參見