Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geolocation working on android 13,14 even 8 but not on android 12 function does not call no error in console #273

Open
adeel399 opened this issue Feb 14, 2024 · 18 comments

Comments

@adeel399
Copy link

Bug Report

Problem

What is expected to happen?

Geolocation should give current location in all android devices

What does actually happen?

but it is working in many devices except android 12 there is no error msg at all function does not even call

Information

Command or Code

this.geolocation.getCurrentPosition().then(async (resp:any) => {

        this.lat=resp.coords.latitude;
        this.long=resp.coords.longitude;
        alert("lat:"+this.lat+"long"+this.long);
        }).catch((error:any) => {
         console.log('Error getting location', error);
         alert("location error:"+error);
       });

Environment, Platform, Device

Android 12 ionic capacitor

Version information

Checklist

  • [x ] I searched for existing GitHub issues
  • [x ] I updated all Cordova tooling to most recent version
  • [ x] I included all the necessary information above
@adeel399
Copy link
Author

i Forget to mention even in android 12 it asked for location permission every thing seems to be fine regarding permission

@breautek
Copy link
Contributor

Are you requesting FINE or COARSE location? (e.g. do you have high accuracy enabled or disabled?)

Android 12 has a known issue where requesting coarse location doesn't work as expected and instead location requests will timeout.

@adeel399
Copy link
Author

const coordinates = await capacitorgeolocation.getCurrentPosition({
enableHighAccuracy: false,
});
right now my code
before i did not added any option (not working in 12)
then i added that option still not working

@adeel399
Copy link
Author

My androidmanifest

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.INTERNET" />

@breautek
Copy link
Contributor

breautek commented Feb 14, 2024

const coordinates = await capacitorgeolocation.getCurrentPosition({ enableHighAccuracy: false, }); right now my code before i did not added any option (not working in 12) then i added that option still not working

Sounds like you're hitting the android bug that I linked earlier. I think the only known workaround is to request high accuracy location (which translates to FINE location natively).

e.g.

const coordinates = await capacitorgeolocation.getCurrentPosition({
enableHighAccuracy: true,
});

When enableHighAccuracy is disabled, then COARSE location is used and location requests is suppose to report on location updates within 1,000km or so. The bug describes a situation where this doesn't work on Android 12. Having enableHighAccuracy enabled makes the underlying API to use FINE location instead which does work as intended on Android 12.

If you don't actually need FINE location, then perhaps it might be worth only enabling it on Android 12 devices.

This comment suggest that the issue is believed to be fixed but it will require an Android OS update and whether that update is available may be subjected to your carrier.

@adeel399
Copy link
Author

So you suggesting enableHighAccuracy:true will resolve the issue on android 12?

@adeel399
Copy link
Author

enableHighAccuracy:true
this does not fix the issue

@breautek
Copy link
Contributor

enableHighAccuracy:true this does not fix the issue

Can you confirm your device location settings?

Settings -> Location -> Location Services

Screenshot from 2024-02-14 14-33-14

Google Location Accuracy should be enabled, otherwise high accuracy / FINE location will not work.

Additionally, the app permission should have Precise enabled:

Settings -> Location -> App location permissions -> Your Application

Screenshot from 2024-02-14 14-35-08

Ensure location access is enabled with precise location.

If either precise location or Google Location Accuracy is disabled, then regardless of the app requesting high accuracy the device will only return COARSE location which will be a victim of the aforementioned android 12 bug.

The app permissions will be set by the user on the first time they use your app, but it's possible they disallow precise permission on the permission prompt which will trigger the android 12 bug. Google Location Accuracy is a user setting that can't be programmatically controlled, the user must enable it to enable high accuracy location. Usually it's enabled by default from factory.

@adeel399
Copy link
Author

ok i ll update you regarding that tommorow

@ckang777
Copy link

ckang777 commented May 24, 2024

@breautek I'm running into the same issue and was wondering if the Android 12 bug affects all cordova builds with cordova Android 12.0.1 platforms?
My users are experiencing the same problem with Samsung S22 Android 14 UI version 6.1.

@breautek
Copy link
Contributor

breautek commented May 25, 2024

@breautek I running into the same issue and was wondering if the Android 12 bug affects all cordova builds with cordova Android 12.0.1 platforms? My users are experiencing the same problem with Samsung S22 Android 14 UI version 6.1.

I believe so, https://issues.chromium.org/issues/40205003 isn't fully addressed yet it seems.

Cordova doesn't actually have an implementation of geolocation for android. It relies on the system webview implementation. So it affects all cordova platforms.

It sounds like there is a fix available but you would be relying on users to update their devices, and sometimes carriers don't deliver AOSP updates, which unfortunately means users will be stuck on a bugged version.

The underlying bug in Android (b/208263743) is supposed to be fixed in the QPR1 update for Android 12, which most devices would have received around the end of 2021, but it's unfortunately not guaranteed that all Android 12 devices will have gotten that update in practice. All Android 13 and later devices will have the fix, though.

So, if you're seeing problems specifically with some Android 12 devices and not other versions then probably those devices just haven't gotten the update. If you're seeing issues on other versions as well then this may need further investigation.

The ticket is still open, I think they are looking to find a solution in the system webview since it got migrated. If they patch a workaround in the webview then that update should make it to all users receiving updates from Google Play Store.

Another potential workaround is using a plugin that implements native geolocation APIs. Don't really know of any that I can vouch for but I'd imagine most native implementations would generally make use of Google's Fuse location APIs, which will tie your app to Google Play services, e.g. your app will only work on Google-enabled devices.

EDIT:

My users are experiencing the same problem with Samsung S22 Android 14 UI version 6.1.

Just caught this... I believe the aforementioned bug only applies Android 12, aka API 31... Not related to Cordova-Android. If you're experiencing this on Android 14/API 34 then you might be experiencing something different.

@ckang777
Copy link

ckang777 commented Jun 1, 2024

@breautek Thank you for your detailed response.

@jdipallo
Copy link

hello,

i am not getting any errors but my android cordova app using this plugin is consistently returning google headquarters in silicon valley. any idea why this might be? oh, this is in the emulator on my mac. my friends android phone puts him Pakistan consistently. This cordova app is built with Android SK 34 and latest 5.0.0 version of the plugin. And, we've used high accuracy as well. The iOS version of this plugin functions correctly. Thank you.

@breautek
Copy link
Contributor

hello,

i am not getting any errors but my android cordova app using this plugin is consistently returning google headquarters in silicon valley. any idea why this might be? oh, this is in the emulator on my mac. my friends android phone puts him Pakistan consistently. This cordova app is built with Android SK 34 and latest 5.0.0 version of the plugin. And, we've used high accuracy as well. The iOS version of this plugin functions correctly. Thank you.

Android emulators are mocked. You need to manually send "location" events via the emulator's Extended Controls.

Screenshot 2024-08-25 at 8 22 07 PM

Then you can choose and set your location which will trigger location update in the emulator.

Screenshot 2024-08-25 at 8 22 55 PM

Physical devices will obviously use GPS hardware, but android emulators does not use GPS hardawre, even if the host has one present afaik.

@jdipallo
Copy link

Hello breautek,
Thank you for reply. I understand regarding the emulator.
Our main issue is on an Android device, the lat/lng is incorrect (Pakistan). This used to work just fine about 1 1/2 ago.

Not exactly sure what has changed.

@breautek
Copy link
Contributor

https://issues.chromium.org/issues/40205003

Cordova doesn't implement any actual geolocation code. What it does do is hook into the webview permission request to request location permissions when necessary. This means geolocation behaviour may change depending on the webview version. It also means that Cordova doesn't have control over geolocation settings at the native level, or influence how it responds to geolocation events. The implementation provided is the exact same implementation found in a standard browser.

So first I'd double check that you're fully updated with the Android System Webview package. This will likely involve signing into Google Play. Google Play Services probably also has some influence on location features, so ensuring that is up to date on your device is also ideal.

If your issue only presents itself with Android 12 devices, then you're likely experiencing https://issues.chromium.org/issues/40205003 (This is the issue that requesting high accuracy is supposedly a workaround). Star the issue so that Google can gauge impact and prioritize appropriately.

If your issue is occurring on devices other than Android 12, or is occurring while high accuracy is being requested (and accepted by the end user) then consider giving my blog post on understanding geolocation accuracy and to attempt to rule out environmental factors. Key takeaways are:

  • Don't be in a concrete basement
  • Ensure A-GPS features are enabled (e.g. allow wifi/celluar/bluetooth scanning in your location settings)
  • Poor weather can also negatively impact geolocation results.

If your still finding that your issue persists in "ideal" geolocation conditions then you'll likely need to raise an issue against chromium, see here for their reporting guidelines.

This issue is open because https://issues.chromium.org/issues/40205003 is still open.

@jdipallo
Copy link

Thank you for the reply.
I am using Meteor JS with Cordova 12.0.1 and Android sdk 34 which is Android 14. After building and creating an Apk file, we load that apk file on an android phone. So, at the moment, no Google Play store in the process yet. And, no errors returned, just simply the wrong location (Pakistan). This phone's location using Google maps successfully obtains the correct geolocation.
This whole scenario worked just fine about 1 1/2 yr ago.
I really don't know where to go from here, especially with no errors or other helpful debug msgs.
Thank you

@breautek
Copy link
Contributor

So, at the moment, no Google Play store in the process yet. And, no errors returned, just simply the wrong location (Pakistan). This phone's location using Google maps successfully obtains the correct geolocation.

Google Play Services != Google Play store. Google Play Services is service for Google APIs which depending on the underlying geolocation implementation being used might use Google APIs (The Fused Location Provider API). I do not know what geolocation implementation the browser actually uses, I'm just making an educated guess since the webview is a Google product.

And, no errors returned, just simply the wrong location (Pakistan).

Just another idea that you can check/confirm... I would confirm that your maximumAge setting is something sensible if it's explicitly set. If it's set to 0 which is also the default, then it instructs the API to always fetch a fresh point from the GPS provider API. If it's a non-zero positive integer, then it may return a cached position. There is no guarentee how fresh the provided location will be, other than it will be within the maximumAge setting. It could give you an older timestamp (potentially one that was erroneous to begin with) even if there is a "fresher" location cached available. The unit is in milliseconds.

If it's explicitly set to something other than 0, I'd consider testing with 0 maximumAge.

And, no errors returned, just simply the wrong location (Pakistan).

Just to point this out there in case if it isn't obvious, this suggest that the API believes it is working as intended. You should also check the accuracy value from the location event. If it's returning a point located in Pakistan but the accuracy reading is a value of 10000, that means that the the true location of the device is believed to be within a 10,000m (ten thousand) radius which is obviously not very accurate.

So if the device is clearly not in Pakistan but the true location is within the accuracy reading of Pakistan, then the API is working as intended.

Reasons for high-value accuracy readings (which suggest that the point is not accurate) will include environmental factors out of your control, but if you're also seeing very high accuracy readings (like 1000m+) then it may also suggest that the FINE location permission isn't actually granted by the user and the only location points you are receiving is COARSE location which is a (very) approximate area.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants