Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native GeoLocation is not working on Android

Can anyone confirm if React Native Geolocation is actually working on Android? I am stuck with location request timed out while using getCurrentPosition() method and no error message while using watchPosition() method.

I have added the required permission (FINE LOCATION) on AndroidManifest.xml and location permission is allowed for the app.

Here is my implementation:

componentWillMount() {
 navigator.geolocation.getCurrentPosition((position) => {
        console.log('+++++++');
        console.log(position.coords.longitude);
        this.setState({
          longitude: position.coords.longitude,
          error: null,
        });
      },
      (error) => {
        console.log(error);

      },

    );
}

React Native Version : 0.42

like image 841
Roshan Gautam Avatar asked Mar 11 '17 07:03

Roshan Gautam


2 Answers

you need to change enableHighAccuracy to false

const LATITUDE_DELTA = 0.0922;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO; 

this.state = {
        region:{}}

 navigator.geolocation.getCurrentPosition(
                (position) => {
                    console.log(position);
                    this.setState({
                        region: {
                            longitude: position.coords.longitude,
                            latitude: position.coords.latitude,
                            latitudeDelta: LATITUDE_DELTA,
                            longitudeDelta: LONGITUDE_DELTA
                        }
                    });
                },
                (error) => console.log(new Date(), error),
                {enableHighAccuracy: false, timeout: 10000, maximumAge: 3000}
            );
like image 77
Nima Avatar answered Sep 27 '22 19:09

Nima


After implementing all above answers i had to restart my new phone then it started working

like image 39
vijay Avatar answered Sep 27 '22 20:09

vijay