react-native-geolocation
react-native-geolocation copied to clipboard
When getCurrentPosition is called?
i wrote this piece of code
componentDidMount(){
Geolocation.getCurrentPosition(info => {
console.log(info.coords.latitude + " " + info.coords.longitude)
this.setState({coords: {latitude: info.coords.latitude, longitude: info.coords.longitude, latitudeDelta: this.LATITUDE_DELTA, longitudeDelta: this.LONGITUDE_DELTA}})
}, error => Alert.alert('Error', JSON.stringify(error)),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000});
console.log(this.state.coords.latitude + " " + this.state.coords.longitude);
region = getapi(this.state.coords.latitude, this.state.coords.longitude)
this.setState({region})
console.log("TEMP: " + region);
hot = getHotBarb(this.state.region)
this.setState({hot})
rated = getRatedBarb(this.state.region)
this.setState({rated})
offer = getOfferBarb(this.state.region)
this.setState({offer})
this.setState({loading: false})
}
But on the log console I see first the log of this.state.coords.latitude + " " + this.state.coords.longitude
(that is undefined) and the log inside the getCurrentPosition
call (that is correct).
The problem is that I can't call the function getapi
because this.state.coords.latitude, this.state.coords.longitude
results undefined
how can i get setState on the getCurrentPosition
function to be called before the other functions?
Hi,
If you want to call getapi
, you can put it inside the getCurrentPosition
.
I think you should read this document to understand the promises in react native.