Geolocation:getCurrentPosition() 方法
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Geolocation
介面的 getCurrentPosition()
方法用於取得裝置的目前位置。
請注意,除了需要安全上下文之外,此功能可能會被 geolocation
Permissions-Policy
封鎖,並且還需要使用者明確授予權限。如有需要,呼叫此方法時會提示使用者。可以使用權限 API 中的 geolocation
使用者權限來查詢權限狀態。
語法
getCurrentPosition(success) getCurrentPosition(success, error) getCurrentPosition(success, error, options)
參數
success
-
一個回呼函式,它接受一個
GeolocationPosition
物件作為其唯一的輸入參數。 error
選擇性-
一個可選的回呼函式,它接受一個
GeolocationPositionError
物件作為其唯一的輸入參數。 options
選擇性-
一個可選的物件,包含以下參數:
maximumAge
選擇性-
一個正的
long
值,表示可接受回傳的快取位置的最大年齡(以毫秒為單位)。如果設定為0
,表示裝置不能使用快取位置,必須嘗試取得真實的目前位置。如果設定為Infinity
,裝置必須回傳快取位置,無論其年齡為何。預設值:0
。 timeout
選擇性-
一個正的
long
值,表示裝置為了回傳位置所允許的最長時間(以毫秒為單位)。預設值為Infinity
,表示getCurrentPosition()
在位置可用之前不會回傳。 enableHighAccuracy
選擇性-
一個布林值,表示應用程式希望接收到最佳的結果。如果為
true
且裝置能夠提供更精確的位置,它就會這麼做。請注意,這可能會導致較慢的回應時間或增加功耗(例如行動裝置上的 GPS 晶片)。另一方面,如果為false
,裝置可以自由地透過更快速的回應和/或使用更少的電力來節省資源。預設值:false
。
回傳值
無(undefined
)。
範例
const options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0, }; function success(pos) { const crd = pos.coords; console.log("你的目前位置是:"); console.log(`緯度:${crd.latitude}`); console.log(`經度:${crd.longitude}`); console.log(`誤差大約 ${crd.accuracy} 公尺。`); } function error(err) { console.warn(`錯誤(${err.code}):${err.message}`); } navigator.geolocation.getCurrentPosition(success, error, options);
規範
Specification |
---|
Geolocation # getcurrentposition-method |