From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../api/geolocation/getcurrentposition/index.html | 134 +++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 files/zh-cn/web/api/geolocation/getcurrentposition/index.html (limited to 'files/zh-cn/web/api/geolocation/getcurrentposition') diff --git a/files/zh-cn/web/api/geolocation/getcurrentposition/index.html b/files/zh-cn/web/api/geolocation/getcurrentposition/index.html new file mode 100644 index 0000000000..27b99b28e7 --- /dev/null +++ b/files/zh-cn/web/api/geolocation/getcurrentposition/index.html @@ -0,0 +1,134 @@ +--- +title: Geolocation.getCurrentPosition() +slug: Web/API/Geolocation/getCurrentPosition +tags: + - API + - Geolocation + - Geolocation API + - Method + - NeedsExample + - Reference +translation_of: Web/API/Geolocation/getCurrentPosition +--- +

{{ APIRef("Geolocation API") }}

+ +

Geolocation.getCurrentPosition() 方法用来获取设备当前位置。

+ +

语法

+ +
navigator.geolocation.getCurrentPosition(success, error, options)
+ +

参数

+ +
+
success
+
成功得到位置信息时的回调函数,使用{{domxref("Position")}} 对象作为唯一的参数。 
+
error {{optional_inline}}
+
获取位置信息失败时的回调函数,使用 {{domxref("PositionError")}} 对象作为唯一的参数,这是一个可选项。 
+
options {{optional_inline}}
+
一个可选的{{domxref("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);
+
+ +

标准

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Geolocation')}}{{Spec2('Geolocation')}}Initial specification.
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5{{CompatGeckoDesktop("1.9.1")}}910.60
+ Removed in 15.0
+ Reintroduced in 16.0
5
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown()}}{{CompatUnknown()}}{{CompatGeckoMobile("4")}}{{CompatUnknown()}}10.60{{CompatUnknown()}}
+
+ +

请参考

+ + -- cgit v1.2.3-54-g00ecf