React Native GeoLocation not working on Android

Can anyone confirm if React Native Geolocation really works on Android? I am stuck with requesting time for placement when using the method getCurrentPosition()and without error message when using the method watchPosition().

I added the required permission (FINE LOCATION) to AndroidManifest.xml and the permission to post is allowed for the application.

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);

      },

    );
}

Real Version: 0.42

+7
source share
6 answers

After implementing all the above answers, I had to restart my new phone, after which it started working

+1
source

, React Native Android.

, getCurrentPosition(..) watchId. watchPosition(..) . , , , . : Android M API PermissionsAndroid.check(...) PermissionsAndroid.request(...).

0

position coords.

 navigator.geolocation.getCurrentPosition(
      ({coords}) => {
        const {latitude, longitude} = coords
        this.setState({
          position: {
            latitude,
            longitude,
          },
          region: {
            latitude,
            longitude,
            latitudeDelta: 0.001,
            longitudeDelta: 0.001,
          }
        })
      },
      (error) => alert(JSON.stringify(error)),
      // 'enableHighAccuracy' sometimes causes problems
      // If it does, just remove it.
      {enableHighAccuracy: true} 
    )
0

enableHighAccuracy 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}
            );
0

I had a similar problem because I was in a building that prevented my device from correctly capturing GPS signals.

0
source

Better if you try this code, I tested it on Android 8 OS, RN 0.49.5 Change the existing current location the trick is to set

{ enableHighAccuracy: true, timeout: 25000, maximumAge: 3600000 }

-1
source

Source: https://habr.com/ru/post/1672039/


All Articles