0

I am trying to get the User's location in an Android React Native application. I've started by following the React Native Geolocation Tutorial but am not having any luck.

I have added the appropriate permissions to the Android project and inserted this code in the react native app:

  componentDidMount() {

    navigator.geolocation.getCurrentPosition(
      (position) => {
        var initialPosition = JSON.stringify(position);
        this.setState({initialPosition});
      },
      (error) => alert(error.message),
      {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
    );
    console.log("Initial position: " + this.state.initialPosition)

  }

I also have the initialPosition variable defined as 'unknown' just like in the tutorial. When the example runs, the initialPosition variable prints unknown. Then about 5 seconds later, the alert shows an 'undefined' error.

I am a bit confused as to what is going on because the error is null. Perhaps I am instantiating the "geolocation" incorrectly? Unfortunatly I am not able to find much information on this through searches so any help is appreciated.

2 Answers 2

2

Hope this can help a bit:

  1. You are logging the initialPosition outside the callback. This means that initialPosition will be unknown because the callback has not been run yet and thus the initialPosition variable has not been updated yet.
  2. Try logging the error variable not just make an alert of error.message. It is possible that there is an error, but not an error.message.
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the tips. Printing the error returned "No available location provider" so at least now I have a direction to go in. Not sure why this wasn't stored in the message..
Have you tried, without enableHighAccuracy: true. I.E: enableHighAccuracy: false?
Turns out I did not have locations enabled on the device. Defiantly not one of my finer hours... Defiantly will need to catch that case in the future!
0
  1. try this code it may be helps you and define latitude and longitude variable in constructor inside this.state as null .
  2. if you want to print json string then define a array in state then use by setting this.setState the array
 - Don't forget to give location permission in manifest file

- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

componentDidMount(){
    navigator.geolocation.getCurrentPosition( (position)=>{
        //const ipos = JSON.stringify(Position);
        this.setState({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
          });
    },
    { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
  );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.