Positioning (C++)

位置

The Positioning component of the Qt Location API is about the geographical position, size, and address of some place. Positioning contains a QGeoCoordinate class, containing latitude, longitude and altitude in meters. QGeoLocation 包含 QGeoCoordinate plus address and size information (a bounding box) so that positions can be more than mathematical points. Movement into or out of the defined bounding box areas can be monitored. The API also allows the developer to control the source of the positional information as well.

Location data involves a precisely specified position on the Earth's surface — as provided by a latitude-longitude coordinate — along with associated data, such as:

  • The date and time at which the position was reported
  • The velocity of the device that reported the position
  • The altitude of the reported position (height above sea level)
  • The bearing of the device in degrees, relative to true north

This data can be extracted through a variety of methods. One of the most well known methods of positioning is GPS (Global Positioning System), a publicly available system that uses radiowave signals received from Earth-orbiting satellites to calculate the precise position and time of the receiver. Another popular method is 'Cell Identifier Positioning', which uses the cell identifier of the cell site that is currently serving the receiving device to calculate its approximate location. These and other positioning methods can all be used with the Location API; the only requirement for a location data source within the API is that it provides a latitude-longitude coordinate with a date/time value, with the option of providing the other attributes listed above.

Location data sources are created by subclassing QGeoPositionInfoSource and providing QGeoPositionInfo objects through the QGeoPositionInfoSource::positionUpdated () signal. Clients that require location data can connect to the positionUpdated () signal and call startUpdates () 或 requestUpdate () to trigger the distribution of location data. The location data distribution can be stopped by calling the stopUpdates () 函数。

A default position source may be available on some platforms. Call QGeoPositionInfoSource::createDefaultSource () to create an instance of the default position source. The method returns nullptr if no default source is available for the platform.

If a problem occurs with access to the information source then an errorOccurred () 信号被发射。

The QGeoAreaMonitorSource class enables client applications to be notified when the receiving device has moved into or out of a particular area, as specified by a coordinate and radius. If the platform provides built-in support for area monitoring, the QGeoAreaMonitorSource::createDefaultSource () method returns an instance of the default area monitor.

Satellite information can also be distributed through the QGeoSatelliteInfoSource 类。调用 QGeoSatelliteInfoSource::createDefaultSource () to create an instance of the default satellite data source for the platform if one is available. Alternatively, clients can subclass it to provide a custom satellite data source.

Requesting Location Data from Data Sources

To receive data from a source, connect to its positionUpdated () signal, then call either startUpdates () 或 requestUpdate () to begin.

Here is an example of a client that receives data from the default location data source, as returned by QGeoPositionInfoSource::createDefaultSource ():

class MyClass : public QObject
{
    Q_OBJECT
public:
    MyClass(QObject *parent = 0)
        : QObject(parent)
    {
        QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(this);
        if (source) {
            connect(source, SIGNAL(positionUpdated(QGeoPositionInfo)),
                    this, SLOT(positionUpdated(QGeoPositionInfo)));
            source->startUpdates();
        }
    }
private slots:
    void positionUpdated(const QGeoPositionInfo &info)
    {
        qDebug() << "Position updated:" << info;
    }
};
					

Controlling Aspects of Data Sources

The QGeoPositionInfoSource::setUpdateInterval () method can be used to control the rate at which position updates are received. For example, if the client application only requires updates once every 30 seconds, it can call setUpdateInterval(30000) . If no update interval is set, or setUpdateInterval () is called with a value of 0, the source uses a default interval or some other internal logic to determine when updates should be provided.

QGeoPositionInfoSource::setPreferredPositioningMethods () enables client applications to request that a certain type of positioning method be used. For example, if the application prefers to use only satellite positioning, which offers fairly precise outdoor positioning but can be a heavy user of power resources, it can call this method with the QGeoPositionInfoSource::SatellitePositioningMethods value. However, this method should only be used in specialized client applications; in most cases, the default positioning methods should not be changed, as a source may internally use a variety of positioning methods that can be useful to the application.

NMEA Data

NMEA is a common text-based protocol for specifying navigational data. For convenience, the QNmeaPositionInfoSource is provided to enable client applications to read and distribute NMEA data in either real-time mode (for example, when streaming from a GPS device) or simulation mode (for example, when reading from a NMEA log file). In simulation mode, the source will emit updates according to the time stamp of each NMEA sentence to produce a "replay" of the recorded data.

Generally, the capabilities provided by the default position source as returned by QGeoPositionInfoSource::createDefaultSource (), along with the QNmeaPositionInfoSource class, are sufficient for retrieving location data. However, in some cases developers may wish to write their own custom location data source.

The Log File Position Source (C++) example demonstrates how to subclass QGeoPositionInfoSource to create a custom positioning source.

范例

Log File Position Source Example

The Log File Position Source Example demonstrates how to subclass QGeoPositionInfoSource to create a custom positioning source.

Weather Info Example

The Weather Info example shows how to use the user's current position to retrieve local content from a web service in a C++ plugin for QML.

Positioning Classes

QGeoAddress 表示 QGeoLocation 的地址
QGeoAreaMonitorInfo 表示接近要监视区域或地区的参数
QGeoAreaMonitorSource Enables the detection of proximity changes for a specified set of coordinates
QGeoCircle Defines a circular geographic area
QGeoCoordinate Defines a geographical position on the surface of the Earth
QGeoLocation Represents basic information about a location
QGeoPath 定义地理路径
QGeoPolygon Defines a geographic polygon
QGeoPositionInfo 包含特定时间点的全局位置、方向和速度的有关聚合信息
QGeoPositionInfoSource Abstract base class for the distribution of positional updates
QGeoRectangle Defines a rectangular geographic area
QGeoSatelliteInfo 包含有关卫星的基本信息
QGeoSatelliteInfoSource Abstract base class for the distribution of satellite information updates
QGeoShape Defines a geographic area
QNmeaPositionInfoSource Positional information using a NMEA data source
QNmeaSatelliteInfoSource Satellite information using an NMEA data source