Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

react-native-geolocation-service getCurrentPosition fails or never returns

ErrorLocation permission is not granted

Short answer

Read the error code in the callback. Code 1 means permission was not granted: on Android the library assumes you already asked. Code 5 means location is off or in the wrong mode. Code 4 means Google Play services are missing. A hang with no error comes from the default timeout, which is infinite.

  • Applies to react-native-geolocation-service 5.x, React Native 0.7x-0.8x, Android 14+, iOS 17+
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the error code and message from the failure callback. The code number maps to the causes below.

    Geolocation.getCurrentPosition(
      (pos) => console.log(pos),
      (err) => console.log(err.code, err.message),
      { timeout: 15000 },
    );
  2. 2Match what you see, then jump to its fix

    1. 1code 1, Location permission is not grantedPermission was never requested (code 1)Fix
    2. 2No prompt on iOS, or permission refused on Android with the manifest unchangedThe native setup is incompleteFix
    3. 3code 5, SETTINGS_NOT_SATISFIED (Android only)Location services are off or in the wrong mode (code 5)Fix
    4. 4code 4, PLAY_SERVICE_NOT_AVAILABLE (Android only)Google Play services are missing or old (code 4)Fix
    5. 5Nothing logs for a long time, or code 3, Location request timed outNo fix arrives and no timeout is set (code 3 or a hang)Fix

What does the error look like?

  • Location permission is not granted
  • Location provider not available
  • Location request timed out
  • Location service is not enabled or location mode is not appropriate for the current request

Cause 1 of 5

Permission was never requested (code 1)

1Check

The README says that on Android the library assumes permission is already granted, so the app must request it first. On iOS the request goes through requestAuthorization.

grep -rn "requestAuthorization\|PermissionsAndroid" src/

2Fix

Ask for permission before the first call. Use PermissionsAndroid on Android and requestAuthorization on iOS.

import { PermissionsAndroid, Platform } from 'react-native';
import Geolocation from 'react-native-geolocation-service';

async function ensurePermission() {
  if (Platform.OS === 'ios') {
    return (await Geolocation.requestAuthorization('whenInUse')) === 'granted';
  }
  const r = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  );
  return r === PermissionsAndroid.RESULTS.GRANTED;
}

3Verify

Call ensurePermission first; getCurrentPosition now returns a position.

ensurePermission().then(console.log);

Confirmed by github.com

Did this fix it?

Cause 2 of 5

The native setup is incomplete

1Check

iOS needs NSLocationWhenInUseUsageDescription in Info.plist. Android needs ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION in AndroidManifest.xml.

grep -n "NSLocation" ios/*/Info.plist
grep -n "ACCESS_.*_LOCATION" android/app/src/main/AndroidManifest.xml

2Fix

Add the missing key or permission, run pod install on iOS, then rebuild the native app.

<!-- AndroidManifest.xml -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

3Verify

Rebuild and request again; the system prompt appears.

grep -n "ACCESS_FINE_LOCATION" android/app/src/main/AndroidManifest.xml

Confirmed by github.com

Did this fix it?

Cause 3 of 5

Location services are off or in the wrong mode (code 5)

1Check

The library shows a dialog asking the user to turn location on when showLocationDialog is true, which is the default. If it was turned off in code, or the user declined, the request fails.

grep -rn "showLocationDialog\|forceRequestLocation" src/

2Fix

Leave showLocationDialog on. Use forceRequestLocation only if you want to request even after the user refuses the accuracy dialog.

Geolocation.getCurrentPosition(onOk, onErr, {
  enableHighAccuracy: true,
  showLocationDialog: true,
  timeout: 15000,
});

3Verify

With location switched off on the device, the dialog appears; after accepting, a position returns.

ensurePermission().then(() => Geolocation.getCurrentPosition(console.log, console.log, { showLocationDialog: true, timeout: 15000 }));

Confirmed by github.com

Did this fix it?

Cause 4 of 5

Google Play services are missing or old (code 4)

1Check

The default provider on Android is the fused provider from Google Play services. A device without a Google Play services install, or with an old version, cannot supply it. The setup page asks for version 11 or higher.

grep -rn "googlePlayServicesVersion" android/build.gradle

2Fix

Use a device or emulator image with Google Play services, or set forceLocationManager to true to use Android's own LocationManager.

Geolocation.getCurrentPosition(onOk, onErr, {
  forceLocationManager: true,
  timeout: 15000,
});

3Verify

A position comes back on the device that failed before.

ensurePermission().then(() => Geolocation.getCurrentPosition(console.log, console.log, { forceLocationManager: true, timeout: 15000 }));

Confirmed by github.com

Did this fix it?

Cause 5 of 5

No fix arrives and no timeout is set (code 3 or a hang)

1Check

The default timeout and maximumAge are both infinite, so a slow first fix can wait forever.

grep -rn "timeout" src/ | grep -i geoloc

2Fix

Set a timeout and accept a cached position up to a chosen age.

Geolocation.getCurrentPosition(onOk, onErr, {
  timeout: 15000,
  maximumAge: 10000,
});

3Verify

A slow fix now ends in a callback within 15 seconds.

ensurePermission().then(() => Geolocation.getCurrentPosition(console.log, console.log, { timeout: 15000, maximumAge: 10000 }));

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Wrap permission, settings and the position call in one helper, and always pass a timeout.
  • Test on a real device as well as on an emulator.
  • Handle every error code in the failure callback and tell the user what to change.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.