Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Getting the location in Qt

From Wiki
Jump to: navigation, search

This code snippet demonstrates how to use the Qt Location API.

Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 5th Edition, Maemo 5
Symbian
Article
Created: tapla (25 May 2010)
Last edited: hamishwillee (11 Oct 2012)

Contents

Qt project file

Link the Location module into the project:

CONFIG += mobility
MOBILITY = location

Using the Location module requires the Location capability:

symbian: {
TARGET.CAPABILITY = Location
}

Header

#include <QGeoPositionInfo>
#include <QGeoPositionInfoSource>
 
 
#include <QDebug>
 
#include <QPointer>
 
// QtMobility namespace
QTM_USE_NAMESPACE
 
class MainWindow : public QMainWindow
{
Q_OBJECT
 
public slots:
/**
* Called when the current position is updated.
*/

void positionUpdated(QGeoPositionInfo geoPositionInfo);
´
private:
/**
* Obtains the location data source and starts listening for position
* changes.
*/

void startGPS();
 
private:
QPointer<QGeoPositionInfoSource> locationDataSource;
QGeoPositionInfo myPositionInfo;
}

Source

#include <QGeoCoordinate>
 
void MainWindow::startGPS()
{
// Obtain the location data source if it is not obtained already
if (!locationDataSource)
{
locationDataSource =
QGeoPositionInfoSource::createDefaultSource(this);
// Whenever the location data source signals that the current
// position is updated, the positionUpdated function is called
connect(locationDataSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
this, SLOT(positionUpdated(QGeoPositionInfo)));
 
// Start listening for position updates
locationDataSource->startUpdates();
}
}
 
void MainWindow::positionUpdated(QGeoPositionInfo geoPositionInfo)
{
if (geoPositionInfo.isValid())
{
// We've got the position. No need to continue the listening.
locationDataSource->stopUpdates();
 
// Save the position information into a member variable
myPositionInfo = geoPositionInfo;
 
// Get the current location as latitude and longitude
QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();
qreal latitude = geoCoordinate.latitude();
qreal longitude = geoCoordinate.longitude();
qDebug() << QString("Latitude: %1 Longitude: %2").arg(latitude).arg(longitude);
}
}

Postconditions

Current location is obtained and printed on the screen.

See also

This page was last modified on 11 October 2012, at 01:23.
50 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×