react-native - watchPosition 的问题反应原生博览会
问题描述
我想在更改位置时发送用户的位置,但这对我不起作用。我曾尝试使用在此平台上找到的其他代码,但我尝试将其转换为spaghetti code。我正在清理它,这是我的代码:
const get_user_id = await AsyncStorage.getItem('user_id');
this.setState({
user_id: get_user_id,
});
navigator.geolocation.watchPosition(
position => {
const locationLat = JSON.stringify(position.coords.latitude);
const locationLong = JSON.stringify(position.coords.longitude);
this.setState({
lat: locationLat,
long: locationLong,
});
fetch(confg.url+'guardaralerta', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
user_id: this.state.user_id,
lat: this.state.lat,
long: this.state.long,
})
}).then((response) => response.json())
.then((responseData) => {
console.log(JSON.stringify(responseData))
});
},
error => Alert.alert(error.message),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
} catch (error) {
console.log(error)
}
解决方案
推荐阅读
- node.js - 在 docker 上向 mongo db 验证用户身份
- java - 从 Path 对象中获取目录名称,末尾不带斜线
- ios - TabBar 未显示 TabBar 项目标题 Swift 5
- r - 从 RStudio 将多个文件上传到 Google Cloud
- php - filesize 必须大于 0 php write files aldough 代码已准备好防止错误
- javascript - 每次运行函数时如何将对象添加到数组中?
- xaml - 如何根据 Xamarin.Forms xaml 中的本地化语言显示选取器项目
- python - 熊猫打印 CSV 文件的问题
- ios - 获取iOS系统默认主题颜色的任何方式
- javascript - 在 QML 中使用 ParseServer 后端 JS 库