QGeoPositionInfoSource Class

The QGeoPositionInfoSource class is an abstract base class for the distribution of positional updates. 更多...

头: #include <QGeoPositionInfoSource>
CMake: find_package(Qt6 COMPONENTS Positioning REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Positioning)
qmake: QT += positioning
Since: Qt 5.2
继承: QObject
继承者: QNmeaPositionInfoSource

公共类型

enum Error { AccessError, ClosedError, NoError, UnknownSourceError, UpdateTimeoutError }
enum PositioningMethod { NoPositioningMethods, SatellitePositioningMethods, NonSatellitePositioningMethods, AllPositioningMethods }
flags PositioningMethods

特性

公共函数

  QGeoPositionInfoSource (QObject * parent )
virtual ~QGeoPositionInfoSource ()
virtual QVariant backendProperty (const QString & name ) const
virtual QGeoPositionInfoSource::Error error () const = 0
virtual QGeoPositionInfo lastKnownPosition (bool fromSatellitePositioningMethodsOnly = false) const = 0
virtual int minimumUpdateInterval () const = 0
QGeoPositionInfoSource::PositioningMethods preferredPositioningMethods () const
virtual bool setBackendProperty (const QString & name , const QVariant & value )
virtual void setPreferredPositioningMethods (QGeoPositionInfoSource::PositioningMethods methods )
virtual void setUpdateInterval (int msec )
QString sourceName () const
virtual QGeoPositionInfoSource::PositioningMethods supportedPositioningMethods () const = 0
int updateInterval () const

公共槽

virtual void requestUpdate (int timeout = 0) = 0
virtual void startUpdates () = 0
virtual void stopUpdates () = 0

信号

void errorOccurred (QGeoPositionInfoSource::Error positioningError )
void positionUpdated (const QGeoPositionInfo & update )
void supportedPositioningMethodsChanged ()

静态公共成员

QStringList availableSources ()
QGeoPositionInfoSource * createDefaultSource (QObject * parent )
QGeoPositionInfoSource * createDefaultSource (const QVariantMap & 参数 , QObject * parent )
QGeoPositionInfoSource * createSource (const QString & sourceName , QObject * parent )
QGeoPositionInfoSource * createSource (const QString & sourceName , const QVariantMap & 参数 , QObject * parent )

详细描述

The static function QGeoPositionInfoSource::createDefaultSource () creates a default position source that is appropriate for the platform, if one is available. Otherwise, QGeoPositionInfoSource will check for available plugins that implement the QGeoPositionInfoSourceFactory 接口。

Users of a QGeoPositionInfoSource subclass can request the current position using requestUpdate (), or start and stop regular position updates using startUpdates () 和 stopUpdates (). When an update is available, positionUpdated () is emitted. The last known position can be accessed with lastKnownPosition ().

If regular position updates are required, setUpdateInterval () can be used to specify how often these updates should be emitted. If no interval is specified, updates are simply provided whenever they are available. For example:

// Emit updates every 10 seconds if available
QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(0);
if (source)
    source->setUpdateInterval(10000);
					

To remove an update interval that was previously set, call setUpdateInterval () with a value of 0.

注意: The position source may have a minimum value requirement for update intervals, as returned by minimumUpdateInterval ().

注意: To use this class from Android service, see Qt Positioning 在 Android .

成员类型文档编制

enum QGeoPositionInfoSource:: Error

The Error enumeration represents the errors which can occur.

常量 描述
QGeoPositionInfoSource::AccessError 0 The connection setup to the remote positioning backend failed because the application lacked the required privileges.
QGeoPositionInfoSource::ClosedError 1 The remote positioning backend closed the connection, which happens for example in case the user is switching location services to off. As soon as the location service is re-enabled regular updates will resume.
QGeoPositionInfoSource::NoError 3 没有出现错误。
QGeoPositionInfoSource::UnknownSourceError 2 发生无法识别的错误。
QGeoPositionInfoSource::UpdateTimeoutError (since Qt 6.2) 4 requestUpdate () was called, this error indicates that the current position could not be retrieved within the specified timeout. If startUpdates () was called, this error indicates that this QGeoPositionInfoSource subclass determined that it will not be able to provide further regular updates. In the latter case the error would not be emitted again until after the regular updates resume.

enum QGeoPositionInfoSource:: PositioningMethod
flags QGeoPositionInfoSource:: PositioningMethods

Defines the types of positioning methods.

常量 描述
QGeoPositionInfoSource::NoPositioningMethods 0x00000000 None of the positioning methods.
QGeoPositionInfoSource::SatellitePositioningMethods 0x000000ff Satellite-based positioning methods such as GPS or GLONASS.
QGeoPositionInfoSource::NonSatellitePositioningMethods 0xffffff00 Other positioning methods such as 3GPP cell identifier or WiFi based positioning.
QGeoPositionInfoSource::AllPositioningMethods 0xffffffff Satellite-based positioning methods as soon as available. Otherwise non-satellite based methods.

The PositioningMethods type is a typedef for QFlags <PositioningMethod>. It stores an OR combination of PositioningMethod values.

特性文档编制

[read-only] minimumUpdateInterval : const int

This property holds the minimum time (in milliseconds) required to retrieve a position update.

This is the minimum value accepted by setUpdateInterval () 和 requestUpdate ().

访问函数:

virtual int minimumUpdateInterval () const = 0

[bindable] preferredPositioningMethods : PositioningMethods

注意: 此特性支持 QProperty 绑定。

Sets the preferred positioning methods for this source.

If new methods include a method that is not supported by the source, the unsupported method will be ignored.

If new methods do not include a single method available/supported by the source, the preferred methods will be set to the set of methods which the source has available. If the source has no method availabe (e.g. because its Location service is turned off or it does not offer a Location service), the passed methods are accepted as they are.

此特性的默认值为 NoPositioningMethods .

注意: Subclass implementations must call the base implementation of setPreferredPositioningMethods() to ensure preferredPositioningMethods() returns the correct value.

另请参阅 supportedPositioningMethods ().

[read-only] sourceName : const QString

This property holds the unique name of the position source implementation in use.

This is the same name that can be passed to createSource () in order to create a new instance of a particular position source implementation.

访问函数:

QString sourceName () const

[bindable] updateInterval : int

注意: 此特性支持 QProperty 绑定。

This property holds the requested interval in milliseconds between each update.

If the update interval is not set (or is set to 0) the source will provide updates as often as necessary.

If the update interval is set, the source will provide updates at an interval as close to the requested interval as possible. If the requested interval is less than the minimumUpdateInterval (), the minimum interval is used instead.

Changes to the update interval will happen as soon as is practical, however the time the change takes may vary between implementations. Whether or not the elapsed time from the previous interval is counted as part of the new interval is also implementation dependent.

The default value for this property is 0.

注意: Subclass implementations must call the base implementation of setUpdateInterval() so that updateInterval() returns the correct value.

成员函数文档编制

QGeoPositionInfoSource:: QGeoPositionInfoSource ( QObject * parent )

Creates a position source with the specified parent .

[signal] void QGeoPositionInfoSource:: errorOccurred ( QGeoPositionInfoSource::Error positioningError )

此信号发射,在发生错误后。 positioningError 参数描述出现错误的类型。

[signal] void QGeoPositionInfoSource:: positionUpdated (const QGeoPositionInfo & update )

startUpdates () 或 requestUpdate () is called, this signal is emitted when an update becomes available.

update value holds the value of the new update.

[pure virtual slot] void QGeoPositionInfoSource:: requestUpdate ( int timeout = 0)

Attempts to get the current position and emit positionUpdated () with this information. If the current position cannot be found within the given timeout (in milliseconds) or if timeout is less than the value returned by minimumUpdateInterval (), an errorOccurred () 信号采用 UpdateTimeoutError is emitted.

If the timeout is zero, the timeout defaults to a reasonable timeout period as appropriate for the source.

This does nothing if another update request is in progress. However it can be called even if startUpdates () has already been called and regular updates are in progress.

If the source uses multiple positioning methods, it tries to get the current position from the most accurate positioning method within the given timeout.

注意: Since Qt6 this method always resets the last error to NoError before requesting the position.

注意: To understand how to use this method from an Android service, see Qt Positioning 在 Android .

[pure virtual slot] void QGeoPositionInfoSource:: startUpdates ()

Starts emitting updates at regular intervals as specified by setUpdateInterval ().

setUpdateInterval () has not been called, the source will emit updates as soon as they become available.

An errorOccurred () 信号采用 UpdateTimeoutError will be emitted if this QGeoPositionInfoSource subclass determines that it will not be able to provide regular updates. This could happen if a satellite fix is lost or if a hardware error is detected. Position updates will recommence if the data becomes available later on. The UpdateTimeoutError error will not be emitted again until after the periodic updates resume.

注意: Since Qt6 this method always resets the last error to NoError before starting the updates.

注意: To understand how to use this method from an Android service, see Qt Positioning 在 Android .

On iOS, starting from version 8, Core Location framework requires additional entries in the application's Info.plist with keys NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription and a string to be displayed in the authorization prompt. The key NSLocationWhenInUseUsageDescription is used when requesting permission to use location services while the app is in the foreground. The key NSLocationAlwaysUsageDescription is used when requesting permission to use location services whenever the app is running (both the foreground and the background). If both entries are defined, NSLocationWhenInUseUsageDescription has a priority in the foreground mode.

[pure virtual slot] void QGeoPositionInfoSource:: stopUpdates ()

Stops emitting updates at regular intervals.

[signal, since Qt 5.12] void QGeoPositionInfoSource:: supportedPositioningMethodsChanged ()

This signal is emitted when the supported positioning methods changed. The cause for a change could be a user turning Location services on/off or restricting Location services to certain types (e.g. GPS only). Note that changes to the supported positioning methods cannot be detected on all platforms. supportedPositioningMethods () provides an overview of the current platform support.

该函数在 Qt 5.12 引入。

[虚拟] QGeoPositionInfoSource:: ~QGeoPositionInfoSource ()

Destroys the position source.

[static] QStringList QGeoPositionInfoSource:: availableSources ()

Returns a list of available source plugins. This includes any default backend plugin for the current platform.

[virtual, since Qt 5.14] QVariant QGeoPositionInfoSource:: backendProperty (const QString & name ) const

Returns the value of the backend-specific property named name , if present. Otherwise, the returned value will be invalid. Supported backend-specific properties are listed and described in Qt Positioning plugins#Default plugins .

该函数在 Qt 5.14 引入。

另请参阅 setBackendProperty .

[static] QGeoPositionInfoSource *QGeoPositionInfoSource:: createDefaultSource ( QObject * parent )

Creates and returns a position source with the given parent that reads from the system's default sources of location data, or the plugin with the highest available priority.

返回 nullptr if the system has no default position source, no valid plugins could be found or the user does not have the permission to access the current position.

[static, since Qt 5.14] QGeoPositionInfoSource *QGeoPositionInfoSource:: createDefaultSource (const QVariantMap & 参数 , QObject * parent )

Creates and returns a position source with the given parent that reads from the system's default sources of location data, or the plugin with the highest available priority.

返回 nullptr if the system has no default position source, no valid plugins could be found or the user does not have the permission to access the current position.

This method passes 参数 to the factory to configure the source.

该函数在 Qt 5.14 引入。

[static] QGeoPositionInfoSource *QGeoPositionInfoSource:: createSource (const QString & sourceName , QObject * parent )

Creates and returns a position source with the given parent , by loading the plugin named sourceName .

返回 nullptr if the plugin cannot be found.

[static, since Qt 5.14] QGeoPositionInfoSource *QGeoPositionInfoSource:: createSource (const QString & sourceName , const QVariantMap & 参数 , QObject * parent )

Creates and returns a position source with the given parent , by loading the plugin named sourceName .

返回 nullptr if the plugin cannot be found.

This method passes 参数 to the factory to configure the source.

该函数在 Qt 5.14 引入。

[pure virtual] QGeoPositionInfoSource::Error QGeoPositionInfoSource:: error () const

返回最后发生的错误类型。

注意: Since Qt6 the last error is always reset when calling startUpdates () 或 requestUpdate ().

[pure virtual] QGeoPositionInfo QGeoPositionInfoSource:: lastKnownPosition ( bool fromSatellitePositioningMethodsOnly = false) const

Returns an update containing the last known position, or a null update if none is available.

fromSatellitePositioningMethodsOnly is true, this returns the last known position received from a satellite positioning method; if none is available, a null update is returned.

[virtual, since Qt 5.14] bool QGeoPositionInfoSource:: setBackendProperty (const QString & name , const QVariant & value )

Sets the backend-specific property named name to value 。返回 true 当成功时, false otherwise. Backend-specific properties can be used to configure the positioning subsystem behavior at runtime. Supported backend-specific properties are listed and described in Qt Positioning plugins#Default plugins .

该函数在 Qt 5.14 引入。

另请参阅 backendProperty .

[pure virtual] QGeoPositionInfoSource::PositioningMethods QGeoPositionInfoSource:: supportedPositioningMethods () const

Returns the positioning methods available to this source. Availability is defined as being usable at the time of calling this function. Therefore user settings like turned off location service or limitations to Satellite-based position providers are reflected by this function. Runtime notifications when the status changes can be obtained via supportedPositioningMethodsChanged ().

Not all platforms distinguish the different positioning methods or communicate the current user configuration of the device. The following table provides an overview of the current platform situation:

平台 简要描述
Android Individual provider status and general Location service state are known and communicated when location service is active.
GeoClue Hardcoced to always return AllPositioningMethods .
GeoClue2 Individual providers are not distinguishable but disabled Location services reflected.
iOS/tvOS Hardcoced to always return AllPositioningMethods .
macOS Hardcoced to always return AllPositioningMethods .
Windows (UWP) Individual providers are not distinguishable but disabled Location services reflected.

另请参阅 supportedPositioningMethodsChanged () 和 setPreferredPositioningMethods ().