我正在使用react-native-sensors
磁力计开发一个指南针应用程序。我已经得到了正确的值,指南针工作得很好,但主要问题是指南针更新速度太快,方向变化频繁且每次变化大约是+-5度。
我希望做一个平滑的方向指南针。
_angle = (magnetometer) => {
if (magnetometer) {
let { x, y, z } = magnetometer
if (Math.atan2(y, x) >= 0) {
angle = Math.atan2(y, x) * (180 / Math.PI)
} else {
angle = (Math.atan2(y, x) + 2 * Math.PI) * (180 / Math.PI)
}
}
return Math.round(angle)
}
//Inside ComponentDidMount
magnetometer.subscribe(({ x, y, z, timestamp }) =>
this.setState({ sensorValue: this._angle({ x, y, z }) })