Retrieve Location Using Qt Mobility: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(LangSwitch-ify)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''English''' [[Retrieve Location Using Qt Mobility Bulgarian|Български]]
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
[[Category:Developing with Qt::QtMobility]]
[[Category:HowTo]]
[[Category:Snippets::Misc]]
[[Category:Tutorial]]


=Retrieve Location Using Qt Mobility=
= Retrieve Location Using Qt Mobility =


==Overview==
== Overview ==


The provided code snippet shows how to retrieve location using Qt Mobility class [http://doc.qt.nokia.com/qtmobility-1.1.0-beta/qgeopositioninfosource.html QGeoPositionInfoSource] ''[doc.qt.nokia.com]''. 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 [http://doc.qt.nokia.com/qtmobility-1.1.0-beta/qgeopositioninfosource.html QGeoPositionInfoSource]. 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 [http://doc.qt.nokia.com/qtmobility-1.1.0-beta/qgeopositioninfosource.html#PositioningMethod-enum positioning methods] ''[doc.qt.nokia.com]'' can be used:
The following [http://doc.qt.nokia.com/qtmobility-1.1.0-beta/qgeopositioninfosource.html#PositioningMethod-enum positioning methods] can be used:
* '''SatellitePositioningMethods''' - Satellite-based positioning methods such as GPS.
* '''NonSatellitePositioningMethods''' - Other positioning methods.
* '''AllPositioningMethods''' - All positioning methods.


* '''SatellitePositioningMethods''' – Satellite-based positioning methods such as <span class="caps">GPS</span>.
The example application is using '''NonSatellitePositioningMethods'''.
* '''NonSatellitePositioningMethods''' – Other positioning methods.
 
* '''AllPositioningMethods''' – All positioning methods.
== 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 [http://developer.qt.nokia.com/wiki/Symbian_Capabilities capability].
 
<code>
symbian:TARGET.CAPABILITY += NetworkServices Location
 
CONFIG += mobility
MOBILITY += location
</code>
 
=== Header ===
 
<code>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <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.
*
* </code>return nothing
*/
void positionUpdated(QGeoPositionInfo geoPositionInfo);


The example application is using '''NonSatellitePositioningMethods'''.
private:
/*
* Start listening for position changes
*
* <code>return nothing
*/
void startLocationAPI();
 
private:
QGeoPositionInfoSource* m_pLocationInfo;
 
QLabel* m_pLabel;
 
};
 
#endif // MAINWINDOW_H
</code>
 
=== Source ===
 
<code>
#include "mainwindow.h"
 
#include <QGeoCoordinate>
#include <QApplication>
#include <QDesktopWidget>
 
#include <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()
{
 
}


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


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


Qt Mobility should be included at the project file. Additionally if the application is targeting Symbian devices Location should be added as a [http://developer.qt.nokia.com/wiki/Symbian_Capabilities capability] ''[developer.qt.nokia.com]''.
// When the position is changed the positionUpdated function is called
connect(m_pLocationInfo, SIGNAL (positionUpdated(QGeoPositionInfo)),
this, SLOT (positionUpdated(QGeoPositionInfo)));


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


===Source===
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();


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


* ‘QGeoPositionInfo’ has not been declared
== Troubleshooting ==
''' '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.


* AllPositioningMethods does not work as expected
<code>
#include <QGeoPositionInfo>
#include <QGeoPositionInfoSource>


This is a known [https://bugreports.qt.io/browse/QTMOBILITY-1550 critical bug that affects Qt Mobility 1.1.2] ''[bugreports.qt.io]'' Please check the provided link for details.
// QtMobility namespace
QTM_USE_NAMESPACE
</code>


==See Also==
* AllPositioningMethods does not work as expected
 
[http://www.developer.nokia.com/Community/Wiki/index.php/Getting_the_location_in_Qt Getting the location in Qt] ''[developer.nokia.com]''


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


* [[:Category:Developing-with-Qt|Developing with Qt]]
== See Also ==
** [[:Category:Developing-with-Qt::QtMobility|QtMobility]]
* [[:Category:HowTo|HowTo]]
* [[:Category:snippets|snippets]]
* [[:Category:Tutorial|Tutorial]]

Latest revision as of 12:09, 28 November 2016

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Retrieve Location Using Qt Mobility

Overview

The provided code snippet shows how to retrieve location using Qt Mobility class QGeoPositionInfoSource. 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 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.

symbian:TARGET.CAPABILITY += NetworkServices Location

CONFIG += mobility
MOBILITY += location

Header

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <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 <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) );
 }
}

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 Please check the provided link for details.

See Also