Geolocation.getCurrentPosition()
方法用來獲取設備當前的位置。
語法
navigator.geolocation.getCurrentPosition(success[, error[, options]])
參數
- success
- 一個回呼函式(callback function) 會被傳入一個
Position
的物件。 - error 選擇性
- 一個選擇性的錯誤回呼函式(callback function),會被傳入一個
PositionError
的物件。 - options 選擇性
- 一個選擇性的
PositionOptions
的物件。
範例
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log('Latitude : ' + crd.latitude);
console.log('Longitude: ' + crd.longitude);
console.log('More or less ' + crd.accuracy + ' meters.');
};
function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
};
navigator.geolocation.getCurrentPosition(success, error, options);
規格
Specification | Status | Comment |
---|---|---|
Geolocation API | Recommendation | Initial specification. |
瀏覽器的相容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 5 | 3.5 (1.9.1) | 9 | 10.60 Removed in 15.0 Reintroduced in 16.0 |
5 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | ? | 4.0 (4) | ? | 10.60 | ? |