Javascript 如何在企业中处理承诺
我是Javascript 如何在企业中处理承诺,javascript,react-native,geolocation,react-native-maps,Javascript,React Native,Geolocation,React Native Maps,我是react native的初学者,目前正试图找到一种解决方法geolocation。我有一个名为region的状态,它用一些值初始化。当一个调用承诺getPosition时,如果它解决了,那么它应该重置区域状态,最后它进行axios调用以获取标记,这意味着如果它解决了,那么标记将被称为新的区域状态,否则将被称为初始状态 然而,当我运行代码时,最后块似乎不起作用。我在代码中使用了watchPosition,它将持续检测我的位置。 请帮助我哪里做错了 以下是我的代码:- index.js con
react native
的初学者,目前正试图找到一种解决方法geolocation
。我有一个名为region
的状态,它用一些值初始化。当一个调用承诺getPosition
时,如果它解决了,那么它应该重置区域
状态,最后它进行axios调用以获取标记
,这意味着如果它解决了,那么标记
将被称为新的区域
状态,否则将被称为初始状态
然而,当我运行代码时,最后
块似乎不起作用。我在代码中使用了watchPosition
,它将持续检测我的位置。
请帮助我哪里做错了
以下是我的代码:-
index.js
const getPosition = (options) => {
return new Promise((resolve, reject) => {
Geolocation.watchPosition(resolve, reject, options);
})
};
const locationOptions = {enableHighAccuracy: true, timeout: 200000, maximumAge: 0};
class Home extends Component {
constructor(props) {
super(props);
this.state = {
region: {
latitude: 17.399320,
longitude: 78.521402,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
},
markers: [],
error: null,
};
this.getMarkers = this.getMarkers.bind(this)
}
getMarkers = () => {
AsyncStorage.getItem('userToken').then((response) => {
const headers = {
'Content-Type': 'application/json',
'x-access-token': response
};
session.get("/common/getIssues", {
params: {
'lat': this.state.region.latitude,
'lng': this.state.region.longitude,
'rad': 5
},
headers: headers,
}).then(response => {
this.setState({markers: response.data.map(issue =>{
return {
latitude:issue.location.lat,
longitude:issue.location.lng
}
} )});
console.log("data is ", this.state.markers)
});
});
};
async componentDidMount() {
this.getMarkers()
getPosition(locationOptions)
.then((position) => {
console.log("inside")
this.setState({
region: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}
})
})
.catch((err) => {
console.log("ewe",err.message)
})
.finally(()=>this.getMarkers())
}
// async componentDidUpdate(prevState) {
// console.log("update event is called")
// const radius = 10;
// if (this.state.region !== prevState.region) {
// this.setState({
// markers: await getIssues(this.state.region.latitude, this.state.region.longitude, radius).then(response => response.data)
// })
// }
// }
render() {
return (
<SafeAreaView>
<ScrollView>
<Container>
<Banner name="Home"/>
<View style={styles.container}>
<MapView
style={styles.map}
provider={PROVIDER_GOOGLE} // remove if not using Google Maps
region={this.state.region}>
{
(this.state.markers) && this.state.markers.map((marker, key) => (
<Marker
key={key}
coordinate={marker}/>)
)
}
</MapView>
</View>
<View style={{flex: 1}}>
<Text style={styles.text}>Filter By Category</Text>
<ScrollView showsHorizontalScrollIndicator={false} horizontal={true}>
{icons.map((object, i) => (
<Button key={i} style={styles.circleShapeView}>
<Ionicon color="#fff" size={35} name={object.icon}/>
<Text style={styles.iconName}>{object.name}</Text>
</Button>
))}
</ScrollView>
</View>
</Container>
</ScrollView>
</SafeAreaView>
);
}
}
export default Home;
const getPosition=(选项)=>{
返回新承诺((解决、拒绝)=>{
地理定位。监视位置(解析、拒绝、选项);
})
};
const locationOptions={enableHighAccurance:true,超时:200000,最大值:0};
类Home扩展组件{
建造师(道具){
超级(道具);
此.state={
地区:{
纬度:17.399320,
经度:78.521402,
纬度德尔塔:0.0922,
纵向德尔塔:0.0421,
},
标记:[],
错误:null,
};
this.getMarkers=this.getMarkers.bind(this)
}
getMarkers=()=>{
AsyncStorage.getItem('userToken')。然后((响应)=>{
常量头={
“内容类型”:“应用程序/json”,
“x-access-token”:响应
};
session.get(“/common/getIssues”{
参数:{
“lat”:this.state.region.latitude,
“lng”:this.state.region.longitude,
“拉德”:5
},
标题:标题,
})。然后(响应=>{
this.setState({markers:response.data.map(issue=>{
返回{
纬度:issue.location.lat,
经度:issue.location.lng
}
} )});
log(“数据是”,this.state.markers)
});
});
};
异步组件didmount(){
这个
getPosition(位置选项)
.然后((位置)=>{
控制台日志(“内部”)
这是我的国家({
地区:{
纬度:位置。坐标。纬度,
经度:position.coords.longitude,
纬度德尔塔:0.0922,
纵向德尔塔:0.0421,
}
})
})
.catch((错误)=>{
console.log(“ewe”,错误消息)
})
.finally(()=>this.getMarkers())
}
//异步组件更新(prevState){
//log(“调用更新事件”)
//常数半径=10;
//if(this.state.region!==prevState.region){
//这是我的国家({
//标记:等待getIssues(this.state.region.latitude、this.state.region.longitude、radius)。然后(response=>response.data)
// })
// }
// }
render(){
返回(
{
(this.state.markers)和&this.state.markers.map((marker,key)=>(
)
)
}
按类别筛选
{icons.map((对象,i)=>(
{object.name}
))}
);
}
}
导出默认主页;
请不要承诺使用值班位置。因为您只能解析1次。此外,当你离开时,你应该删除你的听众
componentDidMount() {
this.watchId = Geolocation.watchPosition(this.handleLocation, error => {}, {});
}
componentWillUnmount() {
Geolocation.clearWatch(this.watchId);
}
handleLocation(location) {
// Do your work here
}