Geolocation
本文档贡献者:sunnylqm(100.00%)
地理定位(Geolocation)API 遵循web 标准。
正因为如此,此 API 和在浏览器上使用方法一致,都是直接访问全局的navigator.geolocation对象,并不需要额外import。
译注 1:本 API 在安卓上需要谷歌框架支持,因而无法在国内使用,请在 github 上搜索百度或高德等国内第三方封装替代库。
译注 2:地理定位只用于返回经纬度数据,无法得出具体地名。如果需要通过经纬度数据查询具体地名,则需要额外的“逆地理编码”(即通过经纬度查询地图数据库得到地名)。一般第三方的地图封装带有此功能。
译注 3:在 iOS 模拟器上,默认定位地址在苹果总部,美国西海岸。
Configuration and Permissions
iOS
你需要在 Info.plist 中增加NSLocationWhenInUseUsageDescription字段来启用定位功能。如果你是用react-native init命令来创建项目,则定位会被默认启用。
In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info.plist and add location as a background mode in the 'Capabilities' tab in Xcode.
如果你是使用 CocoaPods 来引入 React Native,那么请确保你在使用本 API 前引入了RCTGeolocation模块。 sub-podspec.
Android
要请求访问地理位置的权限,你需要在AndroidManifest.xml文件中加入如下一行:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
如果 Android 设备 API 版本>=18(即 Android 4.3 及以上),则位置信息还会包含额外的布尔值mocked,表示此位置信息可能由某服务模拟得出。
如果 Android 设备 API 版本>=18(即 Android 6.0 及以上),则需要额外的步骤来检查权限,即使用PermissionsAndroid API来检查 ACCESS_FINE_LOCATION 权限。 不这么做的话可能导致应用崩溃闪退。
查看方法
文档
方法
setRNConfiguration()
Geolocation.setRNConfiguration(config);
Sets configuration options that will be used in all location requests.
参数:
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
| config | object | 是 | 看下面的说明 |
Supported options:
skipPermissionRequests(boolean, iOS-only) - Defaults tofalse. Iftrue, you must request permissions before using Geolocation APIs.
requestAuthorization()
Geolocation.requestAuthorization();
Request suitable Location permission based on the key configured on pList. If NSLocationAlwaysUsageDescription is set, it will request Always authorization, although if NSLocationWhenInUseUsageDescription is set, it will request InUse authorization.
getCurrentPosition()
Geolocation.getCurrentPosition(geo_success, [geo_error], [geo_options]);
成功时会调用 geo_success 回调,参数中包含最新的位置信息。
参数:
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
| geo_success | function | 是 | 成功时调用,传回最新的位置信息 |
| geo_error | function | 否 | 出错时调用 |
| geo_options | object | 否 | 看下面的说明 |
支持的选项:
timeout(ms)maximumAge(ms) - 默认为无限大。enableHighAccuracy(bool) - On Android, if the location is cached this can return almost immediately, or it will request an update which might take a while.
watchPosition()
Geolocation.watchPosition(success, [error], [options]);
持续监听位置,每当位置变化之后都调用 success 回调。返回一个watchId(整型)。
参数:
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
| success | function | 是 | 位置变化后调用 |
| error | function | 否 | 出错时调用 |
| options | object | 否 | 看下面的说明 |
Supported options:
timeout(ms)maximumAge(ms) - 默认为无限大。enableHighAccuracy(bool)distanceFilter(m)useSignificantChanges(bool)
clearWatch()
Geolocation.clearWatch(watchID);
参数:
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
| watchID | number | 是 | 由watchPosition()方法返回的 id |
stopObserving()
Geolocation.stopObserving();
Stops observing for device location changes. In addition, it removes all listeners previously registered.
Notice that this method has only effect if the geolocation.watchPosition(successCallback, errorCallback) method was previously invoked.