Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Android location request timed out

In IOS there isn't a problem while looking for gps coords. It works fine.

On Android side its not stable as IOS. This problem both in real device and emulator. Sometimes it can find location, but sometimes not. Looking for 3days but there wasn't find a solution.

When my app cannot find my location, I tried via Google Maps app, it works like charm.

Here is my code for both IOS and Android;

getCurrentPosition() {
    navigator.geolocation.getCurrentPosition(
        (position) => {
            this.setState({ initialPosition: position });
            alert(JSON.stringify(position))
            var coords = new Coords();
            coords.Latitude = position.coords.latitude;
            coords.Longitude = position.coords.longitude;
            this.getPharmaciesByCoordinates(coords);
        },
        (error) => alert(error.message),
        { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
    );
    this.watchID = navigator.geolocation.watchPosition((position) => {
        this.setState({ initialPosition: position });
    },
        (error) => alert(error.message),
        { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
    );
}

Any kind of solution is welcome.

Thanks

like image 683
İsmail Şener Avatar asked Dec 13 '16 19:12

İsmail Şener


People also ask

How do I get location react native?

Geolocation is enabled by default when you create a project with react-native init . In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info. plist and add location as a background mode in the 'Capabilities' tab in Xcode.


9 Answers

I removed maximumAge: 3600000 on Android and it working

like image 177
Thai Ha Avatar answered Oct 05 '22 06:10

Thai Ha


It's possible that you are inside of the building or office and your signal is not that good. I've tried with many configuration and this one fit the best for me:

{
    enableHighAccuracy: false,
    timeout: 5000,
    maximumAge: 10000
}

Or try increasing timeout. Because for me the usual 2000ms wasn't working, I kept getting "Location request timed out". So disable high accuracy, increase timeout and you're good to go.

like image 23
dextertron_ Avatar answered Oct 05 '22 08:10

dextertron_


Hi I was facing same issue "request time out ". I have tried every possible solution which I found but, those could not work for me but recently I found one module which overcomes this problem. react-native-geolocation-service This worked for me .....

like image 32
Vishal Pachpande Avatar answered Oct 05 '22 07:10

Vishal Pachpande


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

Geolocation.getCurrentPosition(
      position => {
        const initialPosition = JSON.stringify(position);
        console.log(initialPosition);
      },
      error => Alert.alert('Error', JSON.stringify(error)),
      {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000},
    );

This function work for me and I have change the "AndroidManifest.xml" file with the

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
like image 41
Buddhika Avatar answered Oct 05 '22 06:10

Buddhika


For those who still facing the issue, be aware of the params. I used to leave params to defaults but when it comes to enableHighAccuracy, you should be careful because when you set this to false, that means you are requesting WIFI location else GPS location. If you don't set this true, you will probably receive error on emulators as I did.

Supported options:

timeout (ms) - Is a positive value representing the maximum length of time (in milliseconds) the device is allowed to take in order to return a position. Defaults to INFINITY.

maximumAge (ms) - Is a positive value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return. If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position. If set to Infinity the device will always return a cached position regardless of its age. Defaults to INFINITY.

enableHighAccuracy (bool) - Is a boolean representing if to use GPS or not. If set to true, a GPS position will be requested. If set to false, a WIFI location will be requested.

Detailed Documentation

Edit 1; I recommend you all to use this library instead of native one (react-native-geolocation-service) as its created for such timeout issues.

like image 41
Sabri Meviş Avatar answered Oct 05 '22 06:10

Sabri Meviş


Remove Param 'maximumAge' & Just Keep

{ enableHighAccuracy: true, timeout: 2000}
like image 42
Krunal Vaghela Avatar answered Oct 05 '22 08:10

Krunal Vaghela


I found a solution via external lib that uses native LocationManager. It uses play services location and emitting location event as I want.

Here is the library; https://bitbucket.org/timhagn/react-native-google-locations#readme

And when compiling for android, don't forget to include android/app/build.gradle

dependencies {
    ...
    compile "com.google.android.gms:play-services:10.0.1" -> which version that you have write down here.
    ...
}

And finally don't forget to download Google Play Services and Google Support Library in Android SDK Manager you can find your play services versions into;

<android-sdk-location>/<sdk-version>/extras/google/m2repository/com/google/android/gms/play-services
like image 29
İsmail Şener Avatar answered Oct 05 '22 06:10

İsmail Şener


I have done the app by location request. The command
{ enableHighAccuracy: true, timeout: 25000, maximumAge: 3600000 }, will work on iOS but not in Android. I think you should check Platform.

navigator.geolocation.getCurrentPosition(
          (location) => {
            console.log('location ', location);
            if (this.validLocation(location.coords)) {
              this.locationToAddress(location.coords);
            }
          },
          (error) => {
            console.log('request location error', error);
          },
          Platform.OS === 'android' ? {} : { enableHighAccuracy: true, timeout: 20000, maximumAge: 10000  }
        );
like image 28
Tuan Nguyen Avatar answered Oct 05 '22 06:10

Tuan Nguyen


I tried various methods and npm modules. Finally I got solved this issue by using this module. I strongly suggest this. https://www.npmjs.com/package/react-native-geolocation-service

like image 39
Arjun G Perambra Avatar answered Oct 05 '22 08:10

Arjun G Perambra