Getting GPS location fails with Location provider is unavailable on Android.

Please provide the following:

  1. SDK Version: 42
  2. Platforms(Android/iOS/web/all): Android
  3. Add the appropriate “Tag” based on what Expo library you have a question on.

I have started noticing that getting the device location on Android has started failing frequently. I noticed this when preparing our next release, which contains no code changes related to location.

I built a standalone Expo app with expo-cli and wrote this code in App.js to demonstrate.

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { Button, StyleSheet, View } from 'react-native'
import * as Location from 'expo-location';

const MAX_RETRIES = 50;

const useCurrentLocation = () => {
  const resolve = async () => {
    const response = await Location.requestForegroundPermissionsAsync();

    if (response.status !== 'granted') {
      throw new Error('Insufficient location permissions');
    }

    for (let retryCount = 1; retryCount <= MAX_RETRIES; retryCount += 1) {
      try {
        const { coords } = await Location.getCurrentPositionAsync({
          accuracy: Location.Accuracy.High,
        });

        console.log('Success', coords);
        break;
      } catch (e) {
        if (e.message.match(/^Location provider is unavailable./) && retryCount <= MAX_RETRIES) {
          console.log(
            `Retrying resolving location because location provider is unavailable. (${retryCount} / ${MAX_RETRIES})`
          );
        } else {
          console.error(e);
          break;
        }
      }
    }
  }

  return [ resolve ];
}

export default function App() {
  const [ resolve ] = useCurrentLocation();

  return (
    <View style={styles.container}>
      <Button onPress={resolve} title="Get Location" />
      <StatusBar style="auto" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Getting the location fails many, many times over, sometimes nearly 40 times. Here’s an example output from my Pixel 5. Obviously we’re interested in a solution that’s better than brute force retrying.

Retrying resolving location because location provider is unavailable. (1 / 50)
Retrying resolving location because location provider is unavailable. (2 / 50)
Retrying resolving location because location provider is unavailable. (3 / 50)
Retrying resolving location because location provider is unavailable. (4 / 50)
Retrying resolving location because location provider is unavailable. (5 / 50)
Retrying resolving location because location provider is unavailable. (6 / 50)
Retrying resolving location because location provider is unavailable. (7 / 50)
Retrying resolving location because location provider is unavailable. (8 / 50)
Retrying resolving location because location provider is unavailable. (9 / 50)
Retrying resolving location because location provider is unavailable. (10 / 50)
Retrying resolving location because location provider is unavailable. (11 / 50)
Retrying resolving location because location provider is unavailable. (12 / 50)
Retrying resolving location because location provider is unavailable. (13 / 50)
Retrying resolving location because location provider is unavailable. (14 / 50)
Retrying resolving location because location provider is unavailable. (15 / 50)
Retrying resolving location because location provider is unavailable. (16 / 50)
Retrying resolving location because location provider is unavailable. (17 / 50)
Retrying resolving location because location provider is unavailable. (18 / 50)
Retrying resolving location because location provider is unavailable. (19 / 50)
Retrying resolving location because location provider is unavailable. (20 / 50)
Retrying resolving location because location provider is unavailable. (21 / 50)
Retrying resolving location because location provider is unavailable. (22 / 50)
Retrying resolving location because location provider is unavailable. (23 / 50)
Retrying resolving location because location provider is unavailable. (24 / 50)
Retrying resolving location because location provider is unavailable. (25 / 50)
Retrying resolving location because location provider is unavailable. (26 / 50)
Retrying resolving location because location provider is unavailable. (27 / 50)
Retrying resolving location because location provider is unavailable. (28 / 50)
Retrying resolving location because location provider is unavailable. (29 / 50)
Retrying resolving location because location provider is unavailable. (30 / 50)
Retrying resolving location because location provider is unavailable. (31 / 50)
Retrying resolving location because location provider is unavailable. (32 / 50)
Retrying resolving location because location provider is unavailable. (33 / 50)
Retrying resolving location because location provider is unavailable. (34 / 50)
Retrying resolving location because location provider is unavailable. (35 / 50)
Success Object {
  "accuracy": 11.795999526977539,
  "altitude": 21.178509589978393,
  "altitudeAccuracy": 3,
  "heading": 0,
  "latitude": -37.8253594,
  "longitude": 145.0111059,
  "speed": 0,
}

This issue is also being talked about here, as it seems to have resurfaced unprovoked for a number of people. Seems like about 12 days ago people started seeing this issue.

Thanks

Hi. This was apparently caused by a change that Google made to Google Play Services. The Expo team is working on it:

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.