capacitor: bug: Geolocation on Android always returning 'location unavailable'

Bug Report

Capacitor Version

šŸ’Š   Capacitor Doctor  šŸ’Š 

Latest Dependencies:

  @capacitor/cli: 3.2.0
  @capacitor/core: 3.2.0
  @capacitor/android: 3.2.0
  @capacitor/ios: 3.2.0

Installed Dependencies:

  @capacitor/cli: 3.0.0
  @capacitor/core: 3.0.0
  @capacitor/android: 3.0.0
  @capacitor/ios: 3.1.2

[error] Xcode is not installed
[success] Android looking great! šŸ‘Œ

Platform(s)

Android 10

Current Behavior

When I try to call Geolocation.getCurrentPosition(), I always get an error: ā€˜location unavailable’. I’ve tried this with no options and {enableHighAccuracy: true}.

I have the appropriate permissions set and the code works on PWA and iOS. The app has Location permission while in the foreground and Google Maps can get my location fine.

Expected Behavior

Geolocation.getCurrentPosition() should provide the lat/lng of the current position.

Code Reproduction

edit: basic repo set up here: https://github.com/jonathan-chin/capacitor-geocode-android-example clicking on the get currentposition button will return location unavailable when running on a Google Pixel 3A on Android 10 through Android Studio.

it seems to have been an existing issue in the past #2854 but I am still experiencing it.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 7
  • Comments: 60 (19 by maintainers)

Most upvoted comments

I’ve reported this issue on google issue tracker https://issuetracker.google.com/issues/198176818

Awesome! šŸ˜€

Are there plans to release an update to Capacitor v2 with the same fix? I see someone has an open PR for it: https://github.com/ionic-team/capacitor/pull/4992 . Thought it might be worth asking since some of us have yet to upgrade to v3 and given the level of severity of the issue. šŸ™‚

For now I’ve manually made the changes in https://github.com/ionic-team/capacitor/pull/4992 to node_modules/@capacitor/android/capacitor/src/main/java/com/getcapacitor/plugin/Geolocation.java and used patch-package so we can hotfix our app. We can then remove the patch if the fix lands in Capacitor v2 or when the day comes that we upgrade to v3.

Thanks so much!

We have released @capacitor/android 2.5.0 with the fix, but please, update to capacitor 3 as soon as possible, it’s not that hard and most fixes won’t be backported to capacitor 2.

@capacitor/geolocation 1.1.0 is out with a fix

It’s a Google Play Service bug. I factory reset my phone, the problem is not happening.(Google Play Service v19.5.30) Then update Google play Service to newest version (v21.30.16), the problem happened.

We followed these steps to patch (Thanks @KevinKelchen) and had to add an optional step 6 to have it run in Ionic AppFlow:

  1. Access @capacitor/android and update Geolocation.java using this PRs changes (https://github.com/ionic-team/capacitor/pull/4992/files)
  2. Run npx patch-package @capacitor/android
  3. Update package.json with "postinstall": "patch-package"
  4. npm install patch-package so dependency exists when npm install is run during Ionic AppFlow
  5. Commit the patch file and changes to package.json
  6. (Optional) If you get an error like ā€œcannot run in wd […]ā€ add a .npmrc with unsafe-perm = true when using Ionic AppFlow since we can’t use a non-root user

@KevinKelchen how you use patch-package ? when i run npx patch-package @capacitor/core the output is ā‰ļø There don't appear to be any changes. and patches folder did not created

It’s not @capacitor/core that’s being patched but rather @capacitor/android. Try changing the package name. šŸ™‚

thank you so much, it work now

I can confirm that implementing this change to Geolocation.java fixes it for me

Although effectively this is a change in node_modules , so only a temporary solution… (for now I am using patch-package)

I’m currently migrating my code to use Ionic Native since I have a tight deadline. I’d love to be able to use 100% Capacitor and am happy to switch back after this gets resolved.

I updated the repo a second time to implement @ionic-native geolocation. this gives me a lat/lng while the capacitor implementation still returns ā€˜location unavailable’.