最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make compass in React-Native - Stack Overflow

programmeradmin1浏览0评论

I'am trying to make a pass but i don't know how to use the data of the Magnetometer.

This is my class Compass:

class Compass extends Component {
  constructor(props) {
    super(props);
  }
  ponentWillMount(){
    this._animeRotation = new Animated.Value(0);
  }
  ponentDidMount() {
      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>
    );
  }
}

This is the class App:

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      magn: {
        x: 0,
        y: 0,
        z: 0
      }
    };
  }
  ponentDidMount() {
    //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>
    );
  }
}

With this code the arrow rotate but not as it should. That I have to do?

I'am trying to make a pass but i don't know how to use the data of the Magnetometer.

This is my class Compass:

class Compass extends Component {
  constructor(props) {
    super(props);
  }
  ponentWillMount(){
    this._animeRotation = new Animated.Value(0);
  }
  ponentDidMount() {
      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>
    );
  }
}

This is the class App:

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      magn: {
        x: 0,
        y: 0,
        z: 0
      }
    };
  }
  ponentDidMount() {
    //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>
    );
  }
}

With this code the arrow rotate but not as it should. That I have to do?

Share Improve this question edited Jun 29, 2016 at 10:02 Carlos Quiroga asked Jun 28, 2016 at 13:46 Carlos QuirogaCarlos Quiroga 1,0711 gold badge11 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

The numbers are just relative to currently detected "magnetic north" which is affected by any material with iron (like steel) in the building. So you first have to determine what the true north is.

Also you need to use the X, Y, and Z and get the direction relative to the phone (if it's held horizontally or vertically and which way portrait or landscape). The algorithm is not simple at all.

For this reason, I suggest you use existing packages for getting the direction instead of the Sensor data package. They do the calculations and give you a number with the heading like you seem to be expecting in your animation code. Another possibility is to look into the open source code of these packages and copy the calculations.

If your React Native app is with Expo, like if it was made with the Create React Native App mand (CRNA) then you can use Expo Location. Here's [an example](https://github./martincarrera/expo-location-example on Github) and here are the docs (look for the Heading section).

Otherwise, you can use the React Native simple pass

I ended up making my own package. check it out:

https://github./firofame/react-native-pass-heading

发布评论

评论列表(0)

  1. 暂无评论