Retrieve Location Using Qt Mobility

From Qt Wiki
Revision as of 09:41, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search




[toc align_right="yes" depth="4"]

English Български

Retrieve Location Using Qt Mobility

Overview

The provided code snippet shows how to retrieve location using Qt Mobility class "QGeoPositionInfoSource":http://doc.qt.nokia.com/qtmobility-1.1.0-beta/qgeopositioninfosource.html. When the position is retrieved the location coordinates (latitude and longitude) are displayed on the screen. The given example has been tested on Nokia E7 with Symbian^3.

The following "positioning methods":http://doc.qt.nokia.com/qtmobility-1.1.0-beta/qgeopositioninfosource.html#PositioningMethod-enum can be used:
* SatellitePositioningMethods - Satellite-based positioning methods such as GPS.
* NonSatellitePositioningMethods - Other positioning methods.
* AllPositioningMethods - All positioning methods.

The example application is using NonSatellitePositioningMethods.

Code Snippet

Project File

Qt Mobility should be included at the project file. Additionally if the application is targeting Symbian devices Location should be added as a "capability":http://developer.qt.nokia.com/wiki/Symbian_Capabilities.

<br />symbian:TARGET.CAPABILITY ''= NetworkServices Location
<br />CONFIG''= mobility<br />MOBILITY += location<br />

Header

<br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H

#include &lt;QtGui/QMainWindow&amp;gt;<br />#include &lt;QGeoPositionInfo&amp;gt;<br />#include &lt;QGeoPositionInfoSource&amp;gt;<br />#include &lt;QLabel&amp;gt;

// QtMobility namespace<br />QTM_USE_NAMESPACE

namespace Ui {<br /> class MainWindow;<br />}

class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT<br />public:

explicit MainWindow(QWidget '''parent = 0);
<br /> virtual ~MainWindow();
<br />public slots:<br /> /'''*<br /> * Called when the current position is updated.<br /> *<br /> *

return nothing
/
void positionUpdated(QGeoPositionInfo geoPositionInfo);

private:
/*
* Start listening for position changes
*
*

return nothing<br /> '''/<br /> void startLocationAPI();
<br />private:<br /> QGeoPositionInfoSource''' m_pLocationInfo;

QLabel* m_pLabel;

};

#endif // MAINWINDOW_H<br />

Source

<br />#include &quot;mainwindow.h&amp;quot;

#include &lt;QGeoCoordinate&amp;gt;<br />#include &lt;QApplication&amp;gt;<br />#include &lt;QDesktopWidget&amp;gt;

#include &lt;QtCore/QCoreApplication&amp;gt;

#include &lt;QDebug&amp;gt;

MainWindow::MainWindow(QWidget '''parent)<br /> : QMainWindow(parent), m_pLocationInfo(NULL), m_pLabel(NULL)<br />{<br /> m_pLabel = new QLabel(&quot;&quot;,this);<br /> m_pLabel-&gt;setGeometry(QApplication::desktop()<s>&gt;screenGeometry());<br /> startLocationAPI();<br />}
<br />MainWindow::~MainWindow()<br />{
<br />}
<br />void MainWindow::startLocationAPI()<br />{<br /> // Obtain the location data source if it is not obtained already<br /> if (!m_pLocationInfo)<br /> {<br /> m_pLocationInfo =<br /> QGeoPositionInfoSource::createDefaultSource(this);
<br /> //Select positioning method<br /> m_pLocationInfo</s>&gt;setPreferredPositioningMethods(QGeoPositionInfoSource::NonSatellitePositioningMethods);
<br /> // When the position is changed the positionUpdated function is called<br /> connect(m_pLocationInfo, SIGNAL (positionUpdated(QGeoPositionInfo)),<br /> this, SLOT (positionUpdated(QGeoPositionInfo)));
<br /> // Start listening for position updates<br /> m_pLocationInfo-&gt;startUpdates();<br /> }<br />}
<br />void MainWindow::positionUpdated(QGeoPositionInfo geoPositionInfo)<br />{<br /> if (geoPositionInfo.isValid())<br /> {<br /> // Get the current location coordinates<br /> QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();<br /> qreal latitude = geoCoordinate.latitude();<br /> qreal longitude = geoCoordinate.longitude();
<br /> m_pLabel-&gt;setText( QString(&quot;Latitude: %1 Longitude: %2&amp;quot;).arg(latitude).arg(longitude) );<br /> }<br />}<br />


h2. Troubleshooting
'QGeoPositionInfo' has not been declared

All required header files must be included and the Qt Mobility namespace should be specified.

<br />#include &lt;QGeoPositionInfo&amp;gt;<br />#include &lt;QGeoPositionInfoSource&amp;gt;

// QtMobility namespace<br />QTM_USE_NAMESPACE<br />
  • AllPositioningMethods does not work as expected

This is a known "critical bug that affects Qt Mobility 1.1.2&quot;:https://bugreports.qt.io/browse/QTMOBILITY-1550 Please check the provided link for details.

See Also