react-native-geolocation icon indicating copy to clipboard operation
react-native-geolocation copied to clipboard

When getCurrentPosition is called?

Open FrappaF opened this issue 4 years ago • 1 comments

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?

FrappaF avatar Nov 03 '20 03:11 FrappaF

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.

tai8292 avatar Dec 01 '20 09:12 tai8292