androidgpsreact-nativeandroid-gps

React native queries GPS coordinates and gives timeout


We have the next problem: We are consulting the gps location in order to get a list that contains only those that are close in a radius, but when consulting the GPS it gives us timeout after at leat 40 seconds of waiting. The problem especially occurs in Android with a Xiaomi Redmi 8 in industrial environments, with less connection than usual or on the move, they don´t have access to LTE all the time. We use the library @ react-native-community / geolocation or navigation.position Can you help us with any other library or strategy to obtain GPS position that does not consists in the gps wacthing changes on its position all the time? something like getting it on the background..

Code

Geolocation.getCurrentPosition(
                    position => {
                        // process the position
                    },
                    error => {
                        console.log(error);
                    }, {
                        timeout: 40000,
                        maximumAge: 0
                    }
                );

Solution

  • react-native-community/geolocation does not utilize the more accurate Google Location Services API, as stated in readme

    The recommended library for GPS location (and the one I used) for RN Android is react-native-geolocation-service. Its Readme also mentions a timeout issue which makes me wonder if it is the same as yours:

    This library is created in an attempt to fix the location timeout issue on android with the react-native's current implementation of Geolocation API. This library tries to solve the issue by using Google Play Service's new FusedLocationProviderClient API, which Google strongly recommends over android's default framework location API. It automatically decides which provider to use based on your request configuration and also prompts you to change the location mode if it doesn't satisfy your current request configuration.

    So if I were you I would give it a go. The API is meant to be a drop-in replacement for the library you used so migrating should be just a matter of changing packages.