--- title: Geolocation.getCurrentPosition() slug: Web/API/Geolocation/getCurrentPosition translation_of: Web/API/Geolocation/getCurrentPosition ---
{{ APIRef("Geolocation API") }}
Geolocation.getCurrentPosition()
方法用來獲取設備當前的位置。
navigator.geolocation.getCurrentPosition(success[, error[, options]])
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 |
---|---|---|
{{SpecName('Geolocation')}} | {{Spec2('Geolocation')}} | Initial specification. |
{{ CompatibilityTable() }}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 5 | {{CompatGeckoDesktop("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 | {{CompatUnknown()}} | {{CompatUnknown()}} | {{CompatGeckoMobile("4")}} | {{CompatUnknown()}} | 10.60 | {{CompatUnknown()}} |