Skip to main content
版本:v2

Geolocation

API_LEVEL 2.1 开始支持,API 兼容性请参考 API_LEVEL。

定位传感器。

信息

权限代码: device:os.geolocation

方法​

start​

开始监听定位数据

start(): void

stop​

停止监听定位数据

stop(): void

getStatus​

获取定位状态,返回 A 代表定位中,返回 V 代表无效定位

getStatus(): string

getLatitude​

获取纬度

getLatitude(option: Option): Result

Option​

属性类型必填默认值说明API_LEVEL
formatstring否DD坐标格式,可选 DD 代表十进制或者 DMS 度分秒的形式2.1

Result​

类型说明
number|DMS坐标,坐标系类型 WGS-84

DMS​

属性类型说明API_LEVEL
directionstring方向,N 代表北纬,S 代表南纬2.1
degreesnumber度2.1
minutesnumber分2.1
secondsnumber秒2.1

getLongitude​

获取经度

getLongitude(option: Option): Result

Option​

属性类型必填默认值说明API_LEVEL
formatstring否DD坐标格式,可选 DD 代表十进制或者 DMS 度分秒的形式2.1

Result​

类型说明
number|DMS坐标,坐标系类型 WGS-84

DMS​

属性类型说明API_LEVEL
directionstring方向,E 代表东经,W 代表西经2.1
degreesnumber度2.1
minutesnumber分2.1
secondsnumber秒2.1

onChange​

注册定位信息变化事件监听回调函数

onChange(callback: () => void): void

offChange​

取消定位信息变化事件监听回调函数

offChange(callback: () => void): void

代码示例​

import { Geolocation } from '@zos/sensor'

const geolocation = new Geolocation()

const callback = () => {
if (geolocation.getStatus() === 'A') {
console.log(geolocation.getLatitude())
console.log(geolocation.getLongitude())
}
}

geolocation.start()
geolocation.onChange(callback)

// When not needed for use
geolocation.offChange(callback)
geolocation.stop()