2017-03-08 64 views
-1

我需要保留所有數據。和我的數據有以下:反應原生(IOS)。我的代碼錯誤

時間,2(平均位置),緯度,經度,3(平均加速度)X,Y,Z,

4(平均陀螺儀),X,Y,Z,5 (平均磁強計),x,y,z。

所以,我只是聲明「送出數據」變量

,但我有我的代碼中的一些錯誤。謝謝你的支持。

這是我的代碼。

import React, { Component, PropTypes } from 'react'; 
import { 
    StyleSheet, 
    View, 
    Text, 
    Image, 
    Dimensions, 
    TouchableOpacity, 
    MapView, 
    zoom, 
    DeviceEventEmitter 
} from 'react-native'; 
import {Accelerometer, Gyroscope, Magnetometer} from 'NativeModules'; 
var {height, width} = Dimensions.get('window'); 
import api from './api'; 
import Profile from './Profile'; 
import ScrollableTabView, {DefaultTabBar, ScrollableTabBar } from 'react-native-scrollable-tab-view'; 
import Info from './Info'; 
// import motion from './motion'; 

var d = new Date(); 
var milliseconds = d.getTime(); 
var sendData = [{this.state.time}, 2, {this.state.region.latitude}, {this.state.region.longitude}, 
3, {this.state.Accx}, {this.state.Accy}, {this.state.Accz}, 
4, {this.state.Gyx}, {this.state.Gyy}, {this.state.Gyz}, 
5, {this.state.Magx}, {this.state.Magy}, {this.state.Magz}]; 


export default class Route2 extends Component { 
    constructor(props){ 
    super(props); 
    this.state = { 
     time: '', 
     Accx: 0, 
     Accy: 0, 
     Accz: 0, 
     Gyx: 0, 
     Gyy: 0, 
     Gyz: 0, 
     Magx: 0, 
     Magy: 0, 
     Magz: 0, 
     region: { 
      latitude: 13.980881, 
      longitude: 100.5545009, 
     } 
    }; 

    this.onRegionChange = this.onRegionChange.bind(this); 
} 

componentDidMount() { 

    setInterval(() => { 
     this.setState({ 
     time: new Date().getTime() 
     }); 
    }, 1000); 

    Accelerometer.setAccelerometerUpdateInterval(0.1); // in seconds 
    DeviceEventEmitter.addListener('AccelerationData', (data) => { 
     this.setState({ 
      Accx: data.acceleration.x, 
      Accy: data.acceleration.y, 
      Accz: data.acceleration.z, 
     }); 
    }); 
    Accelerometer.startAccelerometerUpdates(); // you'll start getting AccelerationData events above 

    Gyroscope.setGyroUpdateInterval(0.1); // in seconds 
    DeviceEventEmitter.addListener('GyroData', (data) => { 
     this.setState({ 
      Gyx: data.rotationRate.x, 
      Gyy: data.rotationRate.y, 
      Gyz: data.rotationRate.z, 
     }); 
    }); 
    Gyroscope.startGyroUpdates(); // you'll start getting GyroscopicData events above 

    Magnetometer.setMagnetometerUpdateInterval(0.1); // in seconds 
    DeviceEventEmitter.addListener('MagnetometerData', (data) => { 
     this.setState({ 
      Magx: data.magneticField.x, 
      Magy: data.magneticField.y, 
      Magz: data.magneticField.z, 
     }); 
    }); 
    Magnetometer.startMagnetometerUpdates(); // you'll start getting MagnetomerData events above 
} 

onRegionChange(region) { 
    this.setState({ region }); 
} 


    render() { 
     return (


      <View style={styles.container}> 

     <MapView style={styles.map} 
      mapType="standard" 
      showsUserLocation={true} 
      followsUserLocation={true} 
      showsCompass={false} 
      showsPointOfInterest={false} 
        region={this.state.region} 
      onRegionChange={this.onRegionChange} 
     > 

     </MapView> 
       <View style={styles.container}> 
      <Text> 
        Data: {this.state.time}, 2, {this.state.region.latitude}, {this.state.region.longitude}, 
        3, {this.state.Accx}, {this.state.Accy}, {this.state.Accz}, 
        4, {this.state.Gyx}, {this.state.Gyy}, {this.state.Gyz}, 
        5, {this.state.Magx}, {this.state.Magy}, {this.state.Magz}{'\n'} 
      </Text> 
     </View> 
     </View> 

     ); 
    } 
} 





const styles = StyleSheet.create({ 
    container: { 
     flex: 1, 
     alignItems: 'center', 
     justifyContent: 'center', 
     backgroundColor: 'white' 
    }, 
    image: { 
     width: 200, 
     height: 200, 
    }, 
    text: { 
     color: 'white', 
     fontWeight: 'bold', 
     backgroundColor: 'transparent', 
     marginTop: 20, 
    }, 
    map: { 
     top: 0, 
    width: width, 
    height: height*2/3 
    } 
}); 
+0

什麼是錯誤? '不能得到未定義的狀態? –

+0

關於語法錯誤,我聲明「sendData」 –

回答

0

嘗試宣告sendDataRoute2類中。最可能出現的錯誤是因爲您嘗試通過類聲明外的this訪問Route2狀態。