Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
cpp-position.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page location-positioning-cpp.html
6
7\title Positioning (C++)
8
9\brief The Location Positioning API enables location positioning by means of
10GPS or an NMEA data source.
11
12\section1 Positioning
13
14The Positioning component of the Qt Location API is about the geographical
15position, size, and address of some place. Positioning contains a
16\l QGeoCoordinate class, containing latitude, longitude and altitude in meters.
17\l QGeoLocation contains a \l QGeoCoordinate plus address and size information
18(a bounding box) so that positions can be more than mathematical points.
19Movement into or out of the defined bounding box areas can be monitored. The API
20also allows the developer to control the source of the positional information
21as well.
22
23Location data involves a precisely specified position on the Earth's
24surface \unicode {0x2014} as provided by a latitude-longitude coordinate
25\unicode {0x2014} along with associated data, such as:
26
27 \list
28 \li The date and time at which the position was reported
29 \li The velocity of the device that reported the position
30 \li The altitude of the reported position (height above sea level)
31 \li The bearing of the device in degrees, relative to true north
32 \endlist
33
34This data can be extracted through a variety of methods. One of the most
35well known methods of positioning is GPS (Global Positioning System), a
36publicly available system that uses radiowave signals received from
37Earth-orbiting satellites to calculate the precise position and time of
38the receiver. Another popular method is 'Cell Identifier Positioning', which uses
39the cell identifier of the cell site that is currently serving the receiving
40device to calculate its approximate location. These and other positioning
41methods can all be used with the Location API; the only requirement for a
42location data source within the API is that it provides a
43latitude-longitude coordinate with a date/time value, with the option of
44providing the other attributes listed above.
45
46
47Location data sources are created by subclassing \l QGeoPositionInfoSource and
48providing \l QGeoPositionInfo objects through the
49\l {QGeoPositionInfoSource::positionUpdated()} signal. Clients that require
50location data can connect to the
51\l{QGeoPositionInfoSource::positionUpdated()}{positionUpdated()} signal and
52call \l{QGeoPositionInfoSource::startUpdates()}{startUpdates()} or
53\l{QGeoPositionInfoSource::requestUpdate()}{requestUpdate()} to trigger the
54distribution of location data. The location data distribution can be stopped by
55calling the \l {QGeoPositionInfoSource::stopUpdates()}{stopUpdates()} function.
56
57A default position source may be available on some platforms. Call
58\l {QGeoPositionInfoSource::createDefaultSource()} to create an instance of the
59default position source. The method returns \c nullptr if no default source is
60available for the platform.
61
62If a problem occurs with access to the information source then an
63\l {QGeoPositionInfoSource::errorOccurred()}{errorOccurred()} signal is emitted.
64
65The \l QGeoAreaMonitorSource class enables client applications to be notified
66when the receiving device has moved into or out of a particular area, as
67specified by a coordinate and radius. If the platform provides built-in support
68for area monitoring, the \l {QGeoAreaMonitorSource::createDefaultSource()}
69method returns an instance of the default area monitor.
70
71Satellite information can also be distributed through the
72\l QGeoSatelliteInfoSource class. Call
73\l {QGeoSatelliteInfoSource::createDefaultSource()} to create an instance of the
74default satellite data source for the platform if one is available.
75Alternatively, clients can subclass it to provide a custom satellite data
76source.
77
78
79
80\section2 Requesting Location Data from Data Sources
81
82To receive data from a source, connect to its
83\l{QGeoPositionInfoSource::positionUpdated()}{positionUpdated()} signal,
84then call either \l{QGeoPositionInfoSource::startUpdates()}{startUpdates()}
85or \l{QGeoPositionInfoSource::requestUpdate()}{requestUpdate()} to begin.
86
87Here is an example of a client that receives data from the default location
88data source, as returned by \l {QGeoPositionInfoSource::createDefaultSource()}:
89
90\code
91class MyClass : public QObject
92{
93 Q_OBJECT
94public:
95 MyClass(QObject *parent = 0)
96 : QObject(parent)
97 {
98 QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(this);
99 if (source) {
100 connect(source, SIGNAL(positionUpdated(QGeoPositionInfo)),
101 this, SLOT(positionUpdated(QGeoPositionInfo)));
102 source->startUpdates();
103 }
104 }
105
106private slots:
107 void positionUpdated(const QGeoPositionInfo &info)
108 {
109 qDebug() << "Position updated:" << info;
110 }
111};
112
113\endcode
114
115\section2 Controlling Aspects of Data Sources
116
117The \l {QGeoPositionInfoSource::setUpdateInterval()} method can be used to
118control the rate at which position updates are received. For example, if
119the client application only requires updates once every 30 seconds, it can
120call \c setUpdateInterval(30000). If no update interval is set, or
121\l {QGeoPositionInfoSource::}{setUpdateInterval()} is called with a value of 0,
122the source uses a default interval or some other internal logic to determine
123when updates should be provided.
124
125\l {QGeoPositionInfoSource::setPreferredPositioningMethods()} enables client
126applications to request that a certain type of positioning method be used.
127For example, if the application prefers to use only satellite positioning,
128which offers fairly precise outdoor positioning but can be a heavy user of
129power resources, it can call this method with the
130\l {QGeoPositionInfoSource::SatellitePositioningMethods} value. However, this
131method should only be used in specialized client applications; in most
132cases, the default positioning methods should not be changed, as a source
133may internally use a variety of positioning methods that can be useful to
134the application.
135
136\section2 NMEA Data
137
138\l {http://en.wikipedia.org/wiki/NMEA_0183}{NMEA} is a common text-based
139protocol for specifying navigational data. For convenience, the
140\l QNmeaPositionInfoSource is provided to enable client applications to read
141and distribute NMEA data in either real-time mode (for example, when
142streaming from a GPS device) or simulation mode (for example, when reading
143from a NMEA log file). In simulation mode, the source will emit updates
144according to the time stamp of each NMEA sentence to produce a "replay"
145of the recorded data.
146
147Generally, the capabilities provided by the default position source as
148returned by \l {QGeoPositionInfoSource::createDefaultSource()}, along with the
149\l QNmeaPositionInfoSource class, are sufficient for retrieving location
150data. However, in some cases developers may wish to write their own custom
151location data source.
152
153The \l {Log File Position Source (C++)} example demonstrates how to subclass
154\l QGeoPositionInfoSource to create a custom positioning source.
155
156\section1 Examples
157
158\section3 \b{Log File Position Source Example}
159
160The \l{Log File Position Source (C++)}{Log File Position Source} Example demonstrates
161how to subclass \l QGeoPositionInfoSource to create a custom positioning source.
162
163\section3 \b{Weather Info Example}
164
165The \l{Weather Info} example shows how to use the user's current
166position to retrieve local content from a web service in a C++ plugin for QML.
167
168\section1 Positioning Classes
169
170\annotatedlist QtPositioning-positioning
171
172*/