Qt-UI

Modifications for Retrieving the GPS Position

Running an Android Qt App | | Android Qt on a Samsung Galaxy Ace 2

First, we add another label named “spotLabel” with the integrated Qt Designer.

Then we setup a slot that receives location updates by calling the following setupGPS() method in the MainWindow constructor:

void MainWindow::setupGPS()
{
    // obtain the location data source
    locationInfo_ = QGeoPositionInfoSource::createDefaultSource(this);

    // select positioning method
    locationInfo_->setPreferredPositioningMethods(QGeoPositionInfoSource::AllPositioningMethods);

    // when the position has changed the setGPSLocation slot is called
    connect(locationInfo_, SIGNAL(positionUpdated(QGeoPositionInfo)),
            this, SLOT(setGPSLocation(QGeoPositionInfo)));

    // start listening for position updates
    locationInfo_->startUpdates();
}

And output the updated locations, if any, in lat/lon coordinates:

void MainWindow::setGPSLocation(QGeoPositionInfo geoPositionInfo)
{
    QString text="Location=unknown";

    if (geoPositionInfo.isValid())
    {
        // get the current location coordinates
        QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();

        // transform coordinates to lat/lon
        qreal latitude = geoCoordinate.latitude();
        qreal longitude = geoCoordinate.longitude();

        text=QString("Latitude=%1\nLongitude=%2")
                .arg(latitude,0,'g',8)
                .arg(longitude,0,'g',8);
    }

    ui->spotLabel->setText(text);
}

Add the following line to the .pro file:

 MOBILITY += location sql

Make sure that “Deploy local Qt libraries” is checked again. Once the libraries have been deployed the first time, this option is unchecked automatically. To deploy additional libraries it needs to be checked again.

To get access to the location services we need to add appropriate permissions to the Android manifest.

In the project view, open the “Run” Settings and under “Package Configuration” select the “Permissions” tab. Add the following permissions:

  • ACCESS_COARSE_LOCATION
  • ACCESS_FINE_LOCATION

Don’t forget to save!

Also select the “Libraries” tab and check that QtLocation and QtSql are selected.

We run the app and provide a test gps location by connecting to the emulator via telnet:

telnet localhost 5554

Then we can use the geo command to set a geographic position

geo fix <longitude value> <latitude value>

For example, the geographic position of Kailua, Hawai’i:

geo fix −157.739515 21.397370

For some reason the GPS location slot is not receiving any updates. Instead the updateTimeout signal is emitted. Also, the value of lastKnownPosition can be bogus when the GPS receiver tries to reestablish a lost satellite connection. After excessive reading and testing, giving me major headaches, it appears that Qt Mobility (Qt 4.8) is to be blamed.

As a workaround we connect the following timeout slot to the updateTimeout signal, so that at least we are getting notified that we do not have a GPS position available:

void MainWindow::timeout()
{
    setGPSLocation(locationInfo_->lastKnownPosition());
    locationInfo_->startUpdates();
}

It appears we have to wait for Qt 5.0 to fix the GPS support for the emulator. We’ll follow the latest news to monitor work on Qt5.

Alternatively, we try the app on a real Android device and not the emulator.

Running an Android Qt App | | Android Qt on a Samsung Galaxy Ace 2

Options: