所以基本上我认为只要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>
);
}
}
发布于 2020-11-19 07:17:22
假设您已经导入了以下内容:
import {AsyncStorage} from 'react-native';
点击按钮后,请添加以下代码:
AsyncStorage.setItem('storedlocation', this.state.location);
下一次,如果你想获取asyncstorage的值,比方说进入retrievedlocation的状态,请使用以下代码:
getstoredlocationFunction = () => {
AsyncStorage.getItem('storedlocation').then(
value2 =>
this.setState({ retrievedlocation: value2 })
);
};
祝您今天愉快。
https://stackoverflow.com/questions/64906542
复制