代码如下:
if (this.geomagnetic != null) {
sensor.getRotationMatrix(this.gravityList, this.geomagnetic).then((data: sensor.RotationMatrixResponse) => {
if (data != null) {
sensor.getOrientation(data.rotation).then((data: Array<number>) => {
let orientationData = new OrientationData()
orientationData.sensor = type
orientationData.accuracy = item.a?.toString()
orientationData.orientation = data[0]?.toString()
orientationData.orientationText = this.degreeToDirectionText(data[0])
orientationData.time = Date.now().toString()
if (this.orientationList != null) {
let now = Date.now()
if (this.lastOrientationTime == 0 || (this.lastOrientationTime ?? 0) + 1000 < now) {
this.orientationList.push(orientationData)
this.tryAddOrientationData(false)
this.lastOrientationTime = now
}
}
})
}
})
}
但是获取到的结果,却都是低于2的数值。按照映射规则,则结果都是正北。想要了解一下,HarmonyOS上的朝向信息如何计算,以及计算出来的结果要如何映射到对应的方向上?
可以通过直接订阅方向传感器来获取设备的方向,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-system-sensor-V5\#sensorsubscribedeviceorientation6