跳转到内容

地理位置

{ "name": "blueos.hardware.location.location" }
import geolocation from '@blueos.hardware.location.location' const geolocation = require('@blueos.hardware.location.location')

获取地理位置

精确设备定位

开发者需要在 manifest.json 里面配置权限:

Section titled “开发者需要在 manifest.json 里面配置权限:”
{
"permissions": [{ "name": "watch.permission.LOCATION" }]
}
参数名类型必填说明
timeoutNumber设置超时时间,单位是 ms,默认值为 30000。在权限被系统拒绝或者定位设置不当的情况下,有可能永远不能返回结果,因而需要设置超时。超时后会使用 fail 回调
coordTypeString坐标系类型,可选值可通过 getSupportedCoordTypes 获取,默认为 wgs84
successFunction成功回调
failFunction失败回调,原因可能是用户拒绝
completeFunction执行结束后的回调
参数名类型说明
longitudeNumber经度
latitudeNumber纬度
accuracyNumber精确度
timeNumber时间
错误码说明
400拒绝授予权限
402权限错误(未声明该权限)
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}`)
},
})

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

精确设备定位

参数名类型必填说明
reservedBoolean是否持久化订阅,默认为 false。机制:设置为 true,页面跳转,不会自动取消订阅,需手动取消订阅
coordTypeString坐标系类型,可选值可通过 getSupportedCoordTypes 获取,默认为 wgs84
callbackFunction每次位置信息发生变化,都会被回调
failFunction失败回调,原因可能是用户拒绝
参数名类型说明
longitudeNumber经度
latitudeNumber纬度
accuracyNumber精确度
timeNumber时间
错误码说明
400拒绝授予权限
402权限错误(未声明该权限)
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}`)
},
})

取消监听地理位置

geolocation.unsubscribe()

获取支持的坐标系类型

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

const types = geolocation.getSupportedCoordTypes()