Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当onpress处于活动状态时,如何将位置数据存储到Asyncstorage_Javascript_React Native - Fatal编程技术网

Javascript 当onpress处于活动状态时,如何将位置数据存储到Asyncstorage

Javascript 当onpress处于活动状态时,如何将位置数据存储到Asyncstorage,javascript,react-native,Javascript,React Native,因此,基本上我想在onpress按钮激活时将位置数据存储到异步存储器中。我已经阅读了asyncstorage文档,但是我仍然卡住了,仍然需要一些提示和更多的示例来进行编码 有人能帮我吗 export default class index extends Component { findCoordinates = () => { Geolocation.getCurrentPosition( (position) => { const loca

因此,基本上我想在onpress按钮激活时将位置数据存储到异步存储器中。我已经阅读了asyncstorage文档,但是我仍然卡住了,仍然需要一些提示和更多的示例来进行编码

有人能帮我吗

export default class index extends Component {
  findCoordinates = () => {
    Geolocation.getCurrentPosition(
      (position) => {
        const location = JSON.stringify(position);
        this.setState({ location });
      },
      (error) => Alert.alert(error.message),
      {
        enableHighAccuracy: true,
        timeout: 20000,
        maximumAge: 1000,
        forceRequestLocation: true,
      }
    );
  };

  render() {
    return (
      <Container>
        <ScrollView>
          <View style={styles.top}></View>
          <Card
            containerStyle={{
              borderRadius: 10,
              marginTop: -30,
            }}
          >
            <View>
              <TouchableOpacity onPress={this.findCoordinates}>
                <Text>Find My Coords?</Text>
                <Text>Location: {this.state.location}</Text>
              </TouchableOpacity>
            </View>
          </Card>
        </ScrollView>
      </Container>
    );
  }
}
导出默认类索引扩展组件{
findCoordinates=()=>{
Geolocation.getCurrentPosition(
(职位)=>{
const location=JSON.stringify(位置);
this.setState({location});
},
(错误)=>Alert.Alert(错误消息),
{
EnableHighAccurance:正确,
超时:20000,
最大值:1000,
forceRequestLocation:对,
}
);
};
render(){
返回(
找到我的坐标?
位置:{this.state.Location}
);
}
}

假设您已导入以下内容:


import {AsyncStorage} from 'react-native';

请在单击按钮后添加以下代码:


AsyncStorage.setItem('storedlocation', this.state.location);

 

下次,如果要获取asyncstorage值,请使用以下代码,例如进入“retrievedlocation”状态:


  getstoredlocationFunction = () => {

    AsyncStorage.getItem('storedlocation').then(

      value2 =>

        this.setState({ retrievedlocation: value2 })

    );

  };

祝你今天愉快