Qt Android Precise Location Issue
-
I implemented a class that gives me the GPS-Location for my app. Here a minimal example:
rbManager::rbManager(QObject *parent) : QAbstractListModel(parent) { nm = nullptr; initialized = false; QLocationPermission locationPermission; locationPermission.setAccuracy(QLocationPermission::Precise); if(qApp->checkPermission(locationPermission) != Qt::PermissionStatus::Granted) { qApp->requestPermission(QLocationPermission{}, this, &rbManager::permissionUpdated); } else { qDebug() << "PERMISSION ALREADY GRANTED"; init(); } } void rbManager::permissionUpdated(const QPermission &permission) { if (permission.status() == Qt::PermissionStatus::Granted) { qDebug() << "PERMISSION GRANTED"; init(); } else { qDebug() << "PERMISSION NOT GRANTED"; } }
The init function does something with the gps coordinates. On iOS everything works like expected. However on Android it does not work!
I did everything which was mentioned in the qt6 docs: https://doc.qt.io/qt-6/qlocationpermission.html
I used
QLocationPermission::Precise
as you can see in my C++ code. And I put this<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
in my manifest.
On the console I get this output:
D libCloudLogOffline_arm64-v8a.so: INIT I qt.positioning.android: Unknown positioningMethod I qt.positioning.android: Unknown positioningMethod W qt.positioning.android: : Position data not available due to missing permission
So what I'm doing wrong here?
Edit: Also I should mention, that when the app is installed and started for the first time, I'm asked only for approximate location... Maybe there is a bug in Qt?
-
@myzinsky have you tried to request per mission for Precise and Approximate location ?
-
@myzinsky
I am getting the same error when gettingPrecise
location.
But if useApproximate
it works.Edit: Checking
Precise
location permission also works.The error:
Position data not available due to missing permission
was due to using anactive
qmlPositionSource
without granting the permissions.
I think this could change in the next run :). -
@Mesrine both together don't work ? I mean: at first request Approximate, then Precise
or what happens with both in Manifest and then request only Precise -
Now it is working asking for
Precise
location.
I tried asking firstAproximate
and thenPrecise
but I think once you ask forAproximate
, asking(qApp->checkPermission(lPermission)
) later forPrecise
will giveQt::PermissionStatus::Denied
I have edited my previous comment.
-
So now, I ask for approx. first and then for precise.
I qt.positioning.android: Unknown positioningMethod I qt.positioning.android: Unknown positioningMethod W qt.positioning.android: : Position data not available due to missing permission
still I get this warning, and I don't see the GPS coordinates dropping out as for iOS :-(
-
-
Exactly. In my case the warning
W qt.positioning.android: : Position data not available due to missing permission
was because I had anactive
position source without having granted location permissions.My recommendation is to ask for permission(approximate or precise) first and check that all the properties/bindings that need the coordinates work after the permission is granted.
I qt.positioning.android: Unknown positioningMethod
i do not know where is coming. -
But I think that's what im doing, I check first of all for all permissions and then I initialize positioning with my init() method.
Side note: When I ask for the permissions already at app start, in the constructor of my class, the app crashes because I think the QML cannot be loaded. Therefore I call the
checkPermissions
function before I want to get GPS coordinates.You can see my code here:
1st: QML call: https://github.com/myzinsky/cloudLogOffline/blob/d374acba334ff6cb33fb57bf7ef9d94f2dcb84b5/qml/SettingsView.qml#L197
2nd: C++ processing: https://github.com/myzinsky/cloudLogOffline/blob/d374acba334ff6cb33fb57bf7ef9d94f2dcb84b5/src/repeatermodel.cpp#L51That is the output:
D libCloudLogOffline_arm64-v8a.so: PRECISE PERMISSION GRANTED D libCloudLogOffline_arm64-v8a.so: INIT I qt.positioning.android: Unknown positioningMethod I qt.positioning.android: Unknown positioningMethod W qt.positioning.android: : Position data not available due to missing permission
So permission was granted and I don't get coordinates. Although I do it in the order that you propose.
-
I think this
qApp->requestPermission(QLocationPermission{}, this, &rbManager::precisePermissionUpdated);
should be
qApp->requestPermission(preciselocationPermission, this, &rbManager::precisePermissionUpdated);
With this, you will be asking for precise location permission.
Also
qApp->requestPermission(QLocationPermission{}, this, &rbManager::precisePermissionUpdated);
execute
void rbManager::precisePermissionUpdated(const QPermission &permission)
But I am not sure about the function argumentsconst QPermission &permission
.
Normally what I do is to check for permission again after
qApp->requestPermission
As explained here.
This is how I do it here
Sorry, I did not notice this before on the post.