react-native-geolocation-service getCurrentPosition fails or never returns
Location permission is not grantedShort 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.
Fix it fast
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 }, );2Match what you see, then jump to its fix
- 1
code 1, Location permission is not grantedPermission was never requested (code 1)Fix - 2
No prompt on iOS, or permission refused on Android with the manifest unchangedThe native setup is incompleteFix - 3
code 5, SETTINGS_NOT_SATISFIED (Android only)Location services are off or in the wrong mode (code 5)Fix - 4
code 4, PLAY_SERVICE_NOT_AVAILABLE (Android only)Google Play services are missing or old (code 4)Fix - 5
Nothing 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
- 1
What does the error look like?
Location permission is not grantedLocation provider not availableLocation request timed outLocation 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
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.xml2Fix
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.xmlConfirmed by github.com
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
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.gradle2Fix
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
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 geoloc2Fix
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
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:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Mobile apps
Axios request works on iOS or in a browser but fails on Android with Network Error
AxiosError: Network Error
A React Native app has no CORS, so on Android a Network Error usually means the request never reached the server.
3 causes, each with a fixMobile apps
Axios returns Network Error in a React Native app on iPhone or the iOS Simulator
error.code is ERR_NETWORK and error.response is undefined
A Network Error means iOS stopped the request before any HTTP answer came back.
3 causes, each with a fixMobile apps
backgroundImage: url(...) does nothing in a React Native view
ImageBackground renders an empty area
In React Native the backgroundImage style draws linear and radial gradients, not pictures, so url(...) is ignored.
3 causes, each with a fixMobile apps
A custom font in React Native falls back to the system font
Unrecognized font family 'MyFont'
React Native does not report a missing font, it just draws the system font.
6 causes, each with a fixMobile apps
fetch fails with TypeError: Network request failed in React Native
Network request failed
Network request failed means the request never got an HTTP response, so there is no status code to read.
3 causes, each with a fixMobile apps
A remote image in React Native shows nothing, or only for some URLs
The same component works with one URL and stays blank with another
A network image in React Native is blank for four reasons.
4 causes, each with a fix