# 地理位置 geolocation

# 接口声明

{ "name": "system.geolocation" }
1

# 导入模块

import geolocation from '@system.geolocation'const geolocation = require('@system.geolocation')
1

# 接口定义

# geolocation.getLocation(OBJECT)

获取地理位置

# 权限要求

精确设备定位

# 参数:

参数名 类型 必填 说明
timeout Number 设置超时时间,单位是 ms,默认值为 30000。在权限被系统拒绝或者定位设置不当的情况下,有可能永远不能返回结果,因而需要设置超时。超时后会使用 fail 回调
coordType String 坐标系类型,可选值可通过 getSupportedCoordTypes 获取,默认为 wgs84
success Function 成功回调
fail Function 失败回调,原因可能是用户拒绝
complete Function 执行结束后的回调
# success 返回值:
参数名 类型 说明
longitude Number 经度
latitude Number 纬度
accuracy Number 精确度
time Number 时间
# fail 返回错误代码
错误码 说明
201 用户拒绝,获取定位权限失败
204 超时返回
207 用户拒绝并勾选不再询问复选框
1000 系统位置开关关闭

# 示例:

geolocation.getLocation({
  success: function(data) {
    console.log(
      `handling success: longitude = ${data.longitude}, latitude = ${
        data.latitude
      }`
    )
  },
  fail: function(data, code) {
    console.log(`handling fail, code = ${code}, errorMsg=${data}`)
  }
})
1
2
3
4
5
6
7
8
9
10
11
12

# geolocation.subscribe(OBJECT)

监听地理位置。如果多次调用,仅最后一次调用生效

# 权限要求

精确设备定位

# 参数:

参数名 类型 必填 说明
reserved Boolean 是否持久化订阅,默认为 false。机制:设置为 true,页面跳转,不会自动取消订阅,需手动取消订阅
coordType String 坐标系类型,可选值可通过 getSupportedCoordTypes 获取,默认为 wgs84
callback Function 每次位置信息发生变化,都会被回调
fail Function 失败回调,原因可能是用户拒绝
# callback 返回值:
参数名 类型 说明
longitude Number 经度
latitude Number 纬度
accuracy Number 精确度
time Number 时间
# fail 返回错误代码
错误码 说明
201 用户拒绝,获取定位权限失败
207 用户拒绝并勾选不再询问复选框
1000 系统位置开关关闭

# 示例:

geolocation.subscribe({
  callback: function(data) {
    console.log(
      `handling success: longitude = ${data.longitude}, latitude = ${
        data.latitude
      }`
    )
  },
  fail: function(data, code) {
    console.log(`handling fail, code = ${code}, errorMsg=${data}`)
  }
})
1
2
3
4
5
6
7
8
9
10
11
12

# geolocation.unsubscribe()

取消监听地理位置

# 参数:

# 示例:

geolocation.unsubscribe()
1

# geolocation.getSupportedCoordTypes()

获取支持的坐标系类型

# 参数:

# 返回值:

字符串数组。当前支持的坐标系类型,如['wgs84']

# 示例:

var types = geolocation.getSupportedCoordTypes()
1