QGeoPositionInfoSource Class

QGeoPositionInfoSource 类是用于分发位置更新的抽象基类。更多

头文件: #include <QGeoPositionInfoSource>
CMake: find_package(Qt6 REQUIRED COMPONENTS Positioning)
target_link_libraries(mytarget PRIVATE Qt6::Positioning)
qmake: QT += positioning
继承: QObject
继承于:

QNmeaPositionInfoSource

公共类型

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

属性

公共功能

QGeoPositionInfoSource(QObject *parent)
virtual ~QGeoPositionInfoSource()
(since Qt 5.14) virtual QVariant backendProperty(const QString &name) const
QBindable<QGeoPositionInfoSource::PositioningMethods> bindablePreferredPositioningMethods()
QBindable<int> bindableUpdateInterval()
virtual QGeoPositionInfoSource::Error error() const = 0
virtual QGeoPositionInfo lastKnownPosition(bool fromSatellitePositioningMethodsOnly = false) const = 0
virtual int minimumUpdateInterval() const = 0
QGeoPositionInfoSource::PositioningMethods preferredPositioningMethods() const
(since Qt 5.14) 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)
(since Qt 5.12) void supportedPositioningMethodsChanged()

静态公共成员

QStringList availableSources()
QGeoPositionInfoSource *createDefaultSource(QObject *parent)
(since Qt 5.14) QGeoPositionInfoSource *createDefaultSource(const QVariantMap &parameters, QObject *parent)
QGeoPositionInfoSource *createSource(const QString &sourceName, QObject *parent)
(since Qt 5.14) QGeoPositionInfoSource *createSource(const QString &sourceName, const QVariantMap &parameters, QObject *parent)

详细说明

静态函数QGeoPositionInfoSource::createDefaultSource() 会创建一个适合该平台的默认位置源(如果有的话)。否则,QGeoPositionInfoSource 将检查实现QGeoPositionInfoSourceFactory 接口的可用插件。

QGeoPositionInfoSource 子类的用户可以使用requestUpdate() 请求当前位置,或使用startUpdates() 和stopUpdates() 开始和停止定期位置更新。当更新可用时,positionUpdated() 将被发送。可以使用lastKnownPosition() 访问最后一个已知位置。

如果需要定期更新位置,可以使用setUpdateInterval() 来指定更新的频率。如果没有指定时间间隔,则只要有更新信息就会提供。例如

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

要删除之前设置的更新间隔,请调用setUpdateInterval() 并将其值设置为 0。

注: 位置源可能对更新间隔有最小值要求,如minimumUpdateInterval() 返回的值。

注: 要在 Android 服务中使用该类,请参阅Qt Positioning on Android

成员类型文档

enum QGeoPositionInfoSource::Error

错误枚举表示可能发生的错误。

常量说明
QGeoPositionInfoSource::AccessError0由于应用程序缺乏所需的权限,与远程定位后端的连接设置失败。
QGeoPositionInfoSource::ClosedError1远程定位后端关闭了连接,例如在用户将定位服务切换为关闭的情况下。一旦重新启用定位服务,将恢复正常更新。
QGeoPositionInfoSource::NoError3未发生错误。
QGeoPositionInfoSource::UnknownSourceError2发生不明错误。
QGeoPositionInfoSource::UpdateTimeoutError (since Qt 6.2)4如果调用了requestUpdate() ,该错误表示无法在指定超时内检索到当前位置。如果调用了startUpdates() ,则该错误表示QGeoPositionInfoSource 子类已确定无法提供进一步的定期更新。在后一种情况下,直到恢复定期更新后,该错误才会再次出现。

枚举 QGeoPositionInfoSource::PositioningMethod
标志 QGeoPositionInfoSource::PositioningMethods

定义定位方法的类型。

常量描述
QGeoPositionInfoSource::NoPositioningMethods0x00000000无定位方法。
QGeoPositionInfoSource::SatellitePositioningMethods0x000000ff基于卫星的定位方法,如 GPS 或 GLONASS。
QGeoPositionInfoSource::NonSatellitePositioningMethods0xffffff00其他定位方法,如基于 3GPP 小区标识符或 WiFi 的定位。
QGeoPositionInfoSource::AllPositioningMethods0xffffffff基于卫星的定位方法尽快可用。其他非卫星定位方法。

PositioningMethods 类型是QFlags<PositioningMethod> 的类型定义。它存储定位方法值的 OR 组合。

属性文档

[read-only] minimumUpdateInterval : const int

该属性用于保存获取位置更新所需的最短时间(以毫秒为单位)。

这是setUpdateInterval() 和requestUpdate() 所接受的最小值。

访问函数:

virtual int minimumUpdateInterval() const = 0

[bindable] preferredPositioningMethods : PositioningMethods

注: 此属性支持QProperty 绑定。

设置此源的首选定位方法。

如果新方法中包含来源不支持的方法,则不支持的方法将被忽略。

如果新方法不包括来源可用/支持的单一方法,首选方法将设置为来源可用的方法集。如果信息源没有可用的方法(例如,由于其定位服务已关闭或未提供定位服务),则会按原样接受传递的方法。

此属性的默认值是NoPositioningMethods

注: 子类实现必须调用setPreferredPositioningMethods() 的基本实现,以确保preferredPositioningMethods() 返回正确的值。

另请参阅 supportedPositioningMethods() 。

[read-only] sourceName : const QString

该属性包含正在使用的位置源实现的唯一名称。

该名称可传递给createSource() 以创建特定位置源实现的新实例。

访问函数:

QString sourceName() const

[bindable] updateInterval : int

注意: 该属性支持QProperty 绑定。

该属性用于保存请求的每次更新间隔(以毫秒为单位)。

如果未设置更新间隔(或设置为 0),信息源将根据需要提供更新。

如果设置了更新间隔,源将以尽可能接近请求的间隔提供更新。如果请求的更新间隔小于minimumUpdateInterval() ,则会使用最小更新间隔。

更新间隔的更改将在可行的情况下尽快进行,但更改所需的时间可能因实现方式而异。至于上一间隔的时间是否算作新间隔的一部分,也与实现有关。

此属性的默认值为 0。

注意: 子类实现必须调用setUpdateInterval() 的基本实现,以便updateInterval() 返回正确的值。

注: 该属性不能用于调整 iOS 和 macOS 上的更新频率,因为它们的 API 不提供这种可能性。在这些系统中,该参数仅用于设置UpdateTimeoutError ,如果在所需时间间隔内未收到更新,则触发errorOccurred 信号。

成员函数文档

[explicit] QGeoPositionInfoSource::QGeoPositionInfoSource(QObject *parent)

用指定的parent 创建位置源。

[virtual noexcept] QGeoPositionInfoSource::~QGeoPositionInfoSource()

销毁位置源。

[static] QStringList QGeoPositionInfoSource::availableSources()

返回可用源代码插件列表。其中包括当前平台的任何默认后端插件。

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

如果存在,则返回名为name 的后端特定属性的值。否则,返回值将无效。支持的后端特定属性在Qt Positioning plugins#Default plugins 中列出并描述。

此函数在 Qt 5.14 中引入。

另请参阅 setBackendProperty

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

使用给定的parent 创建并返回一个位置源,该位置源从系统的默认位置数据源或具有最高可用优先级的插件中读取。

如果系统没有默认位置源、找不到有效插件或用户没有访问当前位置的权限,则返回nullptr

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

使用给定的parent 创建并返回一个位置源,该位置源从系统的默认位置数据源或具有最高可用优先级的插件中读取。

如果系统没有默认位置源、找不到有效插件或用户没有访问当前位置的权限,则返回nullptr

该方法将parameters 传递给工厂,以配置位置源。

此函数在 Qt 5.14 中引入。

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

通过加载名为sourceName 的插件,以给定的parent 创建并返回位置源。

如果找不到插件,则返回nullptr

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

通过加载名为sourceName 的插件,以给定的parent 创建并返回位置源。

如果找不到插件,则返回nullptr

此方法将parameters 传递给工厂以配置位置源。

此函数在 Qt 5.14 中引入。

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

返回上次发生的错误类型。

注: 自 Qt6 起,在调用startUpdates() 或requestUpdate() 时,最后一次错误总是会被重置。

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

该信号在发生错误后发出。positioningError 参数描述了发生错误的类型。

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

返回包含最后已知位置的更新,如果没有,则返回空更新。

如果fromSatellitePositioningMethodsOnly 为 true,则返回从卫星定位方法接收到的最后已知位置;如果没有,则返回空更新。

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

如果调用了startUpdates() 或requestUpdate() ,当有更新可用时,就会发出该信号。

update 值包含新更新的值。

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

试图获取当前位置,并根据该信息发出positionUpdated() 信号。如果在给定的timeout (毫秒)内无法找到当前位置,或者timeout 小于minimumUpdateInterval() 返回的值,则会发出包含UpdateTimeoutErrorerrorOccurred() 信号。

如果超时为零,超时默认为适合源的合理超时时间。

如果另一个更新请求正在进行中,则此操作不起任何作用。不过,即使startUpdates() 已被调用,且正在进行常规更新,也可以调用它。

如果源使用多种定位方法,它会尝试在给定的超时时间内从最精确的定位方法中获取当前位置。

注: 自 Qt6 起,在请求位置之前,该方法总是将最后一次错误重置为NoError

注: 要了解如何通过 Android 服务使用此方法,请参阅Qt Positioning on Android

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

将名为name 的后端特定属性设置为value 。成功时返回true ,否则返回false 。后台特定属性可用于在运行时配置定位子系统的行为。支持的后端特定属性在Qt Positioning plugins#Default plugins 中列出并描述。

此函数在 Qt 5.14 中引入。

另请参见 backendProperty

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

开始按照setUpdateInterval() 指定的时间间隔定期发布更新。

如果setUpdateInterval() 未被调用,源将在更新可用时立即发布更新。

如果QGeoPositionInfoSource 子类认为自己无法提供定期更新,则会发出errorOccurred() 信号(UpdateTimeoutError )。这可能发生在卫星定位丢失或检测到硬件错误的情况下。如果稍后数据可用,位置更新将重新开始。直到恢复定期更新后,才会再次发生UpdateTimeoutError 错误。

注: 自 Qt6 起,该方法总是在开始更新前将上次错误重置为NoError

注: 要了解如何在 Android 服务中使用此方法,请参阅Qt Positioning on Android

在 iOS 上,从第 8 版开始,Core Location 框架要求在应用程序的 Info.plist 中添加额外条目,其关键字为 NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 以及将在授权提示中显示的字符串。键 NSLocationWhenInUseUsageDescription 用于请求在应用程序处于前台时使用位置服务的权限。关键字 NSLocationAlwaysUsageDescription 用于在应用程序运行时(前台和后台)请求使用位置服务的权限。如果同时定义了两个条目,NSLocationWhenInUseUsageDescription 在前台模式下具有优先权。

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

停止定期更新。

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

返回该源可用的定位方法。可用性的定义是调用此函数时的可用性。因此,用户设置(如关闭定位服务或对基于卫星的定位提供商的限制)会通过此函数反映出来。当状态发生变化时,可通过supportedPositioningMethodsChanged() 获取运行时通知。

并非所有平台都能区分不同的定位方法,也并非所有平台都能传达设备的当前用户配置。下表概述了当前平台的情况:

平台简要说明
安卓当定位服务处于活动状态时,会知道并传达单个提供商的状态和一般定位服务状态。
地理线索硬设置为始终返回AllPositioningMethods
GeoClue2无法区分单个提供商,但会反映禁用的位置服务。
iOS硬设置为始终返回AllPositioningMethods
macOS硬设置为始终返回AllPositioningMethods
Windows (UWP)无法区分单个提供商,但已禁用位置服务。

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

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

当支持的定位方法发生变化时,就会发出该信号。发生变化的原因可能是用户打开/关闭了定位服务或将定位服务限制为某些类型(如仅 GPS)。请注意,无法在所有平台上检测到支持的定位方法发生变化。supportedPositioningMethods() 提供了当前平台支持的概览。

此功能在 Qt 5.12 中引入。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.