react native navigator.geolocation.getCurrentPosition not working
Asked Answered
D

5

13

I am using a real android device (version 5.1)

I am able to determine my position using react-native-maps (correct blue point position on map inside my app ) + I am able use google maps app and go to my position (GPS works).

I am using a big timeout, toggling enableHighAccuracy to true and false, remove options ... etc . all failed to get navigator.geolocation to get data.

Here is my code:

var options = {
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0
};

function success(pos) {
  var crd = pos.coords;

  console.log('Your current position is:');
  console.log(`Latitude : ${crd.latitude}`);
  console.log(`Longitude: ${crd.longitude}`);
  console.log(`More or less ${crd.accuracy} meters.`);
};

function error(err) {
  console.warn(`ERROR(${err.code}): ${err.message}`);
};

navigator.geolocation.getCurrentPosition(success, error, options);

I am getting : ERROR(3): Location request timed out

Dietitian answered 16/11, 2017 at 16:3 Comment(0)
P
12

I know it's too late, but it can help someone else.

Geolocation has been extracted from react native .60 version. If you need an alternative solution

Install react-native-community/geolocation :

npm install @react-native-community/geolocation --save

react-native link @react-native-community/geolocation

Then, to request access to location, you need to add the following line to your app's AndroidManifest.xml

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

Usage :

import Geolocation from '@react-native-community/geolocation';

Geolocation.getCurrentPosition(info => console.log(info));
Piccaninny answered 1/11, 2019 at 8:30 Comment(2)
what's about {enableHighAccuracy: true, timeout: 20000, maximumAge: 10000} ?Beeswing
its a weird I don't know where is the best explanation why we can't get the right coordinates using this geolocation current positionPolyvinyl
P
10

Two things.

1) That is actually not a high timeout for a high accuracy response if the coordinates are not cached 2) Some devices have problems with the highAccuracy setting.

Try putting 30000 ms as timeout and remove the high accuracy until you find one setting that works.

Edit: I found the long bug I was remembering form React Native: https://github.com/facebook/react-native/issues/7495

So, try this:

1) Remove maximumAge property 2) If that does not work, remove the third parameter and use the default values from the native module. Meaning, don't send the options object. That should work.

Paola answered 17/11, 2017 at 2:17 Comment(10)
thank you for your reply. I just paste the code from here :developer.mozilla.org/fr/docs/Web/API/Geolocation/… . but i am using different settings ` { enableHighAccuracy: false, timeout: 50000, maximumAge: 0 }` and failed.Dietitian
I am getting this result : imgur.com/a/JL40m . few seconds after opening the page. But I am unable to get any data programmatically.Dietitian
I dont believe that is high acurracy since the diameter is too great. Do you have gps enabled in your phone? Have you given the app the access to the fine location permissoons? (in manifest and in the security page of the app)Paola
I have toggled enableHighAccuracy to false and true. and yes, I have add : <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> to AndroidManifest.xml file and I am still getting the same error. I am willing to share code if you are interested.Dietitian
Also I have used: 5000, 50000 and 500000 values for timeout. and I am still getting the same error. If i enable gps on my phone the blue point of my position shows in 2 seconds with exact position of mine. and with a diameter of around 200 metersDietitian
Remove the maximumAge setting and try again. Not 0, just remove it.Paola
See my edited answer. I remembered what the bug was.Paola
I have tried by removing maximumAge and options all together and I am getting the same error: ERROR(3): Location request timeout . I was the last one to comment on that github issue. I have shared the link to my code on github. that would be great if you take a look at it! Thanks a lot !Dietitian
I have discovered that I am facing exactly this issue. (#35126849) . getCurrentPosition failed. watchPosition works ! I still don't know why.Dietitian
For me, adding <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> in android manifest file fixed the issue..Oday
C
5

Removing maximumAge solved the issue here! Just in case someone is still having this problem in 2019 or later

Colewort answered 9/6, 2019 at 22:54 Comment(0)
S
3

To request access to location, you need to add the following line to your app's AndroidManifest.xml: <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />.

For an android with API >= 23 you need to require an additional step to check for: you need to request the ACCESS_FINE_LOCATION permission using the PermissionsAndroid API.

PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        title: "Location Accessing Permission",
        message: "App needs access to your location"
      }
    );

And only after that run navigator.geolocation.getCurrentPosition(success, error, options);

Shelly answered 8/2, 2019 at 7:6 Comment(0)
D
1

I used this and it works fine: {enableHighAccuracy: false, timeout: 50000}

async watchPosition() {
    console.log('watchPosition');        
    await navigator.geolocation.getCurrentPosition(
        (position) => {
          console.log('Position -> ',position);
        },
        (error) => console.log(error)
        {enableHighAccuracy: false, timeout: 50000}
    );
}
Dunn answered 13/10, 2018 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.