Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Retrieve Location Using Qt Mobility: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:Developing with Qt::QtMobility]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]
[[Category:Developing with Qt::QtMobility]]
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Tutorial]]


[toc align_right=&quot;yes&amp;quot; depth=&quot;4&amp;quot;]
[toc align_right="yes" depth="4"]


'''English''' [[Retrieve_Location_Using_Qt_Mobility_Bulgarian|Български]]
'''English''' [[Retrieve_Location_Using_Qt_Mobility_Bulgarian|Български]]
Line 9: Line 12:
== Overview ==
== Overview ==


The provided code snippet shows how to retrieve location using Qt Mobility class &quot;QGeoPositionInfoSource&amp;quot;: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 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 &quot;positioning methods&amp;quot;:http://doc.qt.nokia.com/qtmobility-1.1.0-beta/qgeopositioninfosource.html#PositioningMethod-enum can be used:<br />* '''SatellitePositioningMethods''' - Satellite-based positioning methods such as GPS.<br />* '''NonSatellitePositioningMethods''' - Other positioning methods.<br />* '''AllPositioningMethods''' - All positioning methods.
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'''.
The example application is using '''NonSatellitePositioningMethods'''.
Line 19: Line 25:
=== Project File ===
=== 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 &quot;capability&amp;quot;:http://developer.qt.nokia.com/wiki/Symbian_Capabilities.
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.


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


=== Header ===
=== Header ===


<code><br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H
<code>
#ifndef MAINWINDOW_H
#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;
#include <QtGui/QMainWindow>
#include <QGeoPositionInfo>
#include <QGeoPositionInfoSource>
#include <QLabel>


// QtMobility namespace<br />QTM_USE_NAMESPACE
// QtMobility namespace
QTM_USE_NAMESPACE


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


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


explicit MainWindow(QWidget '''parent = 0);
explicit MainWindow(QWidget '''parent = 0);
<br /> virtual ~MainWindow();
 
<br />public slots:<br /> /'''*<br /> * Called when the current position is updated.<br /> *<br /> * </code>return nothing<br /> '''/<br /> void positionUpdated(QGeoPositionInfo geoPositionInfo);
virtual ~MainWindow();
<br />private:<br /> /'''*<br /> * Start listening for position changes<br /> *<br /> * <code>return nothing<br /> '''/<br /> void startLocationAPI();
 
<br />private:<br /> QGeoPositionInfoSource''' m_pLocationInfo;
public slots:
/'''*
* Called when the current position is updated.
*
* </code>return nothing
*/
void positionUpdated(QGeoPositionInfo geoPositionInfo);
 
private:
/'''*
* Start listening for position changes
*
* <code>return nothing
*/
void startLocationAPI();
 
private:
QGeoPositionInfoSource''' m_pLocationInfo;


QLabel* m_pLabel;
QLabel* m_pLabel;
Line 46: Line 84:
};
};


#endif // MAINWINDOW_H<br /></code>
#endif // MAINWINDOW_H
</code>


=== Source ===
=== Source ===


<code><br />#include &quot;mainwindow.h&amp;quot;
<code>
#include "mainwindow.h"
 
#include <QGeoCoordinate>
#include <QApplication>
#include <QDesktopWidget>
 
#include <QtCore/QCoreApplication>
 
#include <QDebug>
 
MainWindow::MainWindow(QWidget '''parent)
: QMainWindow(parent), m_pLocationInfo(NULL), m_pLabel(NULL)
{
m_pLabel = new QLabel("",this);
m_pLabel->setGeometry(QApplication::desktop()->screenGeometry());
startLocationAPI();
}
 
MainWindow::~MainWindow()
{
 
}
 
void MainWindow::startLocationAPI()
{
// Obtain the location data source if it is not obtained already
if (!m_pLocationInfo)
{
m_pLocationInfo =
QGeoPositionInfoSource::createDefaultSource(this);
 
//Select positioning method
m_pLocationInfo->setPreferredPositioningMethods(QGeoPositionInfoSource::NonSatellitePositioningMethods);
 
// When the position is changed the positionUpdated function is called
connect(m_pLocationInfo, SIGNAL (positionUpdated(QGeoPositionInfo)),
this, SLOT (positionUpdated(QGeoPositionInfo)));
 
// Start listening for position updates
m_pLocationInfo->startUpdates();
}
}


#include &lt;QGeoCoordinate&amp;gt;<br />#include &lt;QApplication&amp;gt;<br />#include &lt;QDesktopWidget&amp;gt;
void MainWindow::positionUpdated(QGeoPositionInfo geoPositionInfo)
{
if (geoPositionInfo.isValid())
{
// Get the current location coordinates
QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();
qreal latitude = geoCoordinate.latitude();
qreal longitude = geoCoordinate.longitude();


#include &lt;QtCore/QCoreApplication&amp;gt;
m_pLabel->setText( QString("Latitude: %1 Longitude: %2").arg(latitude).arg(longitude) );
}
}
</code>


#include &lt;QDebug&amp;gt;
h2. Troubleshooting


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 />}
''' 'QGeoPositionInfo' has not been declared
<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 /></code>
<br />h2. Troubleshooting
<br />''' 'QGeoPositionInfo' has not been declared


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


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


// QtMobility namespace<br />QTM_USE_NAMESPACE<br /></code>
// QtMobility namespace
QTM_USE_NAMESPACE
</code>


* AllPositioningMethods does not work as expected
* AllPositioningMethods does not work as expected


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


== See Also ==
== See Also ==

Revision as of 10:02, 25 February 2015


[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.

symbian:TARGET.CAPABILITY ''= NetworkServices Location

CONFIG''= mobility
MOBILITY += location

Header

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QGeoPositionInfo>
#include <QGeoPositionInfoSource>
#include <QLabel>

// QtMobility namespace
QTM_USE_NAMESPACE

namespace Ui {
 class MainWindow;
}

class MainWindow : public QMainWindow
{
 Q_OBJECT
public:

explicit MainWindow(QWidget '''parent = 0);

 virtual ~MainWindow();

public slots:
 /'''*
 * Called when the current position is updated.
 *
 *

return nothing

*/
void positionUpdated(QGeoPositionInfo geoPositionInfo);

private:

/*
* Start listening for position changes
*

*

return nothing
 */
 void startLocationAPI();

private:
 QGeoPositionInfoSource''' m_pLocationInfo;

QLabel* m_pLabel;

};

#endif // MAINWINDOW_H

Source

#include "mainwindow.h"

#include <QGeoCoordinate>
#include <QApplication>
#include <QDesktopWidget>

#include <QtCore/QCoreApplication>

#include <QDebug>

MainWindow::MainWindow(QWidget '''parent)
 : QMainWindow(parent), m_pLocationInfo(NULL), m_pLabel(NULL)
{
 m_pLabel = new QLabel("",this);
 m_pLabel->setGeometry(QApplication::desktop()->screenGeometry());
 startLocationAPI();
}

MainWindow::~MainWindow()
{

}

void MainWindow::startLocationAPI()
{
 // Obtain the location data source if it is not obtained already
 if (!m_pLocationInfo)
 {
 m_pLocationInfo =
 QGeoPositionInfoSource::createDefaultSource(this);

 //Select positioning method
 m_pLocationInfo->setPreferredPositioningMethods(QGeoPositionInfoSource::NonSatellitePositioningMethods);

 // When the position is changed the positionUpdated function is called
 connect(m_pLocationInfo, SIGNAL (positionUpdated(QGeoPositionInfo)),
 this, SLOT (positionUpdated(QGeoPositionInfo)));

 // Start listening for position updates
 m_pLocationInfo->startUpdates();
 }
}

void MainWindow::positionUpdated(QGeoPositionInfo geoPositionInfo)
{
 if (geoPositionInfo.isValid())
 {
 // Get the current location coordinates
 QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();
 qreal latitude = geoCoordinate.latitude();
 qreal longitude = geoCoordinate.longitude();

 m_pLabel->setText( QString("Latitude: %1 Longitude: %2").arg(latitude).arg(longitude) );
 }
}

h2. Troubleshooting

'QGeoPositionInfo' has not been declared

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

#include <QGeoPositionInfo>
#include <QGeoPositionInfoSource>

// QtMobility namespace
QTM_USE_NAMESPACE
  • AllPositioningMethods does not work as expected

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

See Also