Car*_*oga 5 javascript ecmascript-6 react-jsx react-native
我试图制作指南针,但我不知道如何使用磁力计的数据.
这是我的班级指南针:
class Compass extends Component {
constructor(props) {
super(props);
}
componentWillMount(){
this._animeRotation = new Animated.Value(0);
}
componentDidMount() {
this.startAnimation();
}
startAnimation() {
Animated.timing(this._animeRotation, {
toValue: this.props.magn, //<-- What put here?
duration: 1000,
}).start(() => {
this.startAnimation();
});
}
render() {
var interpolatedRotateAnimation = this._animeRotation.interpolate({
inputRange: [0, 100],
outputRange: ['0deg', '360deg']
});
return (
<View>
<Animated.View style={[styles.box, {transform: [{rotate: interpolatedRotateAnimation}]}]}>
<Image style={styles.box} source={require('./arrow.png')}></Image>
</Animated.View>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是类App:
class App extends Component {
constructor(props) {
super(props);
this.state = {
magn: {
x: 0,
y: 0,
z: 0
}
};
}
componentDidMount() {
//I can get the gyroscope if is necessary
SensorManager.startMagnetometer(500);
// magn is a object with axis z,y and x
DeviceEventEmitter.addListener('Magnetometer', (magn) => this.setState({magn}));
}
render() {
return (
<View>
<Compass magn={this.state.magn.x}></Compass>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,箭头旋转但不应该旋转.我必须这样做?
这些数字仅相对于当前检测到的“磁北”而言,“磁北”受到建筑物中任何含铁材料(如钢)的影响。因此,您首先必须确定真正的北方是什么。
此外,您还需要使用 X、Y 和 Z 并获取相对于手机的方向(如果手机水平或垂直握持,以及纵向或横向方向)。算法一点也不简单。
因此,我建议您使用现有的包来获取方向,而不是使用传感器数据包。他们进行计算并为您提供一个带有标题的数字,就像您在动画代码中所期望的那样。另一种可能性是研究这些包的开源代码并复制计算。
如果您的 React Native 应用程序与 Expo 一起使用,例如它是使用Create React Native App
命令 (CRNA) 制作的,那么您可以使用Expo Location
. 这是[一个示例](Github 上的https://github.com/martincarrera/expo-location-example ),这里是文档(查找标题部分)。
否则,您可以使用React Native 简单指南针