react-native-geolocation: Android - Location request timed out

Environment

System:
    OS: macOS Mojave 10.14.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 59.02 MB / 32.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 12.14.0 - ~/.config/nvm/12.14.0/bin/node
    Yarn: 1.21.1 - /usr/local/bin/yarn
    npm: 6.13.4 - ~/.config/nvm/12.14.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.6010548
    Xcode: 11.2.1/11B500 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.12.0 => 16.12.0
    react-native: 0.61.5 => 0.61.5
  npmGlobalPackages:
    react-native-cli: 2.0.1

Platforms

Android

Versions

Description

I use code

  return new Promise(resolve =>
    /* global navigator */
    Geolocation.getCurrentPosition(
      (...args) => {
        console.log('getCurrentPosition', args);
        // debugger;
        resolve(true);
      },
      err => {
        console.log('getCurrentPosition.error', err);
        // debugger;
        resolve(false);
      },
      {
        // enableHighAccuracy: true,
        // timeout: 20000,
        maximumAge: 1000,
      },
    ),
  );

and it gives me

PERMISSION_DENIED: 1
POSITION_UNAVAILABLE: 2
TIMEOUT: 3
code: 3
message: "Location request timed out"

I checked settings - location services is on, permissions provided (plus other package react-native-background-geolocation ) get’s location fine

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 21
  • Comments: 22

Most upvoted comments

It works by removing enableHighAcurracy and maximumAge

Try to use:

enableHighAccuracy: false

=(

It’s an issue has been here for a long time. It’s a serious issue for basic usage of the lib. getCurrentPosition has been used in +90% of the cases and it doesn’t work properly on Android.

work for me

Geolocation.getCurrentPosition((success)=>{console.log(success)}, (e)=>{console.log(e)}, {timeout: 20000});

I have tried many ways to fix this issue and non of them properly worked. It works on most of devices but in some cases its not even work on them and the error is always location request timed out. This is a serious bug and there is no proper error information, plus no permission or Google Play Services check in Android devices.

image

I am going to take a chance and try this library as its created based on this issue.

UPDATE: I am writing this after almost 2 years of this comment and received no issue after using react-native-geolocation-service.

Try to use:

enableHighAccuracy: false

=(

Worked for me!

    const getUserCurrentLocation = () => {
        let latitude, longitude

        Geocoder.init(google_api_key)
        Geolocation.getCurrentPosition(
            info => {
                const { coords } = info

                latitude = coords.latitude
                longitude = coords.longitude

                setLat(latitude)
                setLng(longitude)

                getUserCurrentAddress(latitude, longitude)
            },
            error => console.log(error),
            {
                enableHighAccuracy: false,
                timeout: 2000,
                maximumAge: 3600000
            }
        )
    }

NOTE: Attention to the order of the parameters passed to the function “Geolocation.getCurrentPosition”! They must be in that order: success function, error function and parameters.

For me i found that removing the maximumAge solves the timeout issue

Same issue… In this post I read that this issue occurs depending on Android SDK version. Can anyone tell me if this is true? If so the lib should mention so in the docs, right?

I tested it on an AVD simulator with SDK 29 and it worked fine… But when I test on a real device with android 8.1.0 this doesn’t work and my request times out…

Removing the [maximumAge] doesn’t solve it for me and setting [enableHighAccuracy] to false is a no-op for me.

Try it out: useEffect(()=>{ setTimeOut(() => { getCurrentPosition() },1000) },[])

, error => console.log(error), { enableHighAccuracy: false, timeout: 2000, maximumAge: 3600000 }

jazakALLAH dudes this helps me to solve the above error happy coding 😃

This worked for me too! Thanks mate @danielcnascimento

The main problem is, if you turn off enableHighAccuracy, position determination by geolocation is downgrading. I met this problem several times, and always had a problem