geolocation-error-code-3
A workaround to get over the issue: "Geolocation timed out. Code 3". I just worked with Android OS only.
In React Native docs
Normally when we develop React Native apps and want to get current position of the user. This is the code that Facebook provides in the documentation:
navigator.geolocation.getCurrentLocation(successCallback, failureCallback, options)
If you develop an Android app with RN and use Genymotion emulators:
- Remember to add permission to the
AndroidManifest.xml
file:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- Remember to enable GPS, the button at the right side of the emulator
After doing those setup and still get the error {error: 'Geolocation timeout', code: 3}
(or something like that, sorry I don't remember exactly the error message), you are not alone. There are many people got the error as well.
See this issue in React Native repository.
As you can see in the issue, there are some trick as:
- Change
enableHighAccuracy
tofalse
, but actually the result was not as expected, the accuracy was very low. - Use the lib react-native-geolocation-service instead of directly calling
getCurrentLocation
fromnavigator.geolocation
- Remove the third parameter of the
getCurrentLocation
(removeoptions
parameter) I did try them all, but usually get the error-code-3 as mentioned above (actually sometimes I still could get the location, but mostly get the error)
So I tried to do this way:
- Get location from
getCurrentLocation
as Facebook suggested (with highAccuracy enabled) - If w get an error, we will try
navigator.geolocation.watchPosition
and thennavigator.geolocation.clearWatch
after successfully get the location.
It did work.
So, if you find this method is not good or error-prone, just let me know.