The PositionSource type provides the device's current position. More...
Import Statement: | import QtPositioning |
Since: | Qt 5.2 |
The PositionSource type provides information about the user device's current position. The position is available as a Position type, which contains all the standard parameters typically available from GPS and other similar systems, including longitude, latitude, speed and accuracy details.
As different position sources are available on different platforms and devices, these are categorized by their basic type (Satellite, NonSatellite, and AllPositioningMethods). The available methods for the current platform can be enumerated in the supportedPositioningMethods property.
To indicate which methods are suitable for your application, set the preferredPositioningMethods property. If the preferred methods are not available, the default source of location data for the platform will be chosen instead. If no default source is available (because none are installed for the runtime platform, or because it is disabled), the valid property will be set to false.
The updateInterval property can then be used to indicate how often your application wishes to receive position updates. The start(), stop() and update() methods can be used to control the operation of the PositionSource, as well as the active property, which when set is equivalent to calling start() or stop().
When the PositionSource is active, position updates can be retrieved either by simply using the position property in a binding (as the value of another item's property), or by providing an implementation of the onPositionChanged
signal-handler.
The following example shows a simple PositionSource used to receive updates every second and print the longitude and latitude out to the console.
PositionSource { id: src updateInterval: 1000 active: true onPositionChanged: { var coord = src.position.coordinate; console.log("Coordinate:", coord.longitude, coord.latitude); } }
The GeoFlickr example application shows how to use a PositionSource in your application to retrieve local data for users from a REST web service.
As it's mentioned above, PositionSource provides two ways to control its operation state:
Note: It's very important not to mix these approaches. If a bindable active property is used to control the PositionSource object, but later start() or stop() is called from the other part of the code, the binding is broken, which may result in, for example, a UI element that is not connected to any underlying object anymore.
Consider the following example of bad code where the active
property is bound to the CheckBox state, and calling stop() in the onClicked
signal handler breaks that binding.
Window { width: 640 height: 480 visible: true PositionSource { id: posSource name: "geoclue2" active: cb.checked } Column { anchors.centerIn: parent spacing: 20 CheckBox { id: cb } Button { id: btn text: "Stop" onClicked: { posSource.stop() } } } }
Once the Stop button is clicked, stop() is executed, and the binding for the active property is broken. At this point the CheckBox UI element is no longer controlling the PositionSource object.
A straightforward fix in this case is to update the CheckBox state from the onClicked
handler. As soon as the CheckBox is unchecked, the active property will be notified, and the PositionSource object's state will update accordingly. The UI will also be in a consistent state.
Button { id: btn text: "Stop" onClicked: { cb.checked = false } }
Note: Using update() to request a single position update does not have any effect on the active property's bindings, so they can be used together without any problems.
See also QtPositioning::Position, QGeoPositionInfoSource, PluginParameter, and Qt Bindable Properties.
active : bool
This property indicates whether the position source is active. Setting this property to false equals calling stop, and setting this property true equals calling start.
See also start, stop, and update.
name : string
This property holds the unique internal name for the plugin currently providing position information.
Setting the property causes the PositionSource to use a particular positioning provider. If the PositionSource is active at the time that the name property is changed, it will become inactive. If the specified positioning provider cannot be loaded the position source will become invalid.
Changing the name property may cause the updateInterval, supportedPositioningMethods and preferredPositioningMethods properties to change as well.
[default, since QtPositioning 5.14] parameters : list<PluginParameter>
This property holds the list of plugin parameters.
This property was introduced in QtPositioning 5.14.
position : Position
This property holds the last known positional data. It is a read-only property.
The Position type has different positional member variables, whose validity can be checked with appropriate validity functions (for example sometimes an update does not have speed or altitude data).
However, whenever a positionChanged
signal has been received, at least position::coordinate::latitude, position::coordinate::longitude, and position::timestamp can be assumed to be valid.
See also start, stop, and update.
preferredPositioningMethods : enumeration
This property holds the preferred positioning methods of the current source.
sourceError : enumeration
This property holds the error which last occurred with the PositionSource.
supportedPositioningMethods : enumeration
This property holds the supported positioning methods of the current source.
updateInterval : int
This property holds the desired interval between updates (milliseconds).
See also QGeoPositionInfoSource::updateInterval().
valid : bool
This property is true if the PositionSource object has acquired a valid backend plugin to provide data. If false, other methods on the PositionSource will have no effect.
Applications should check this property to determine whether positioning is available and enabled on the runtime platform, and react accordingly.
[since Qt Positioning 5.14]
Variant backendProperty(string name)
Returns the value of the backend-specific property named name, if present. Otherwise, including if called on an uninitialized PositionSource, the return value will be invalid. Supported backend-specific properties are listed and described in Qt Positioning plugins#Default plugins.
This method was introduced in Qt Positioning 5.14.
See also backendProperty and QGeoPositionInfoSource::setBackendProperty.
[since Qt Positioning 5.14]
bool setBackendProperty(string name, Variant value)
Sets the backend-specific property named name to value. Returns true on success, false otherwise, including if called on an uninitialized PositionSource. Supported backend-specific properties are listed and described in Qt Positioning plugins#Default plugins.
This method was introduced in Qt Positioning 5.14.
See also backendProperty and QGeoPositionInfoSource::setBackendProperty.
start()
Requests updates from the location source. Uses updateInterval if set, default interval otherwise. If there is no source available, this method has no effect.
Note: Calling this method breaks the bindings of active property.
See also stop, update, and active.
stop()
Stops updates from the location source. If there is no source available or it is not active, this method has no effect.
Note: Calling this method breaks the bindings of active property.
See also start, update, and active.
update(int timeout)
A convenience method to request single update from the location source. If there is no source available, this method has no effect.
If the position source is not active, it will be activated for as long as it takes to receive an update, or until the request times out. The request timeout period is source-specific.
The timeout is specified in milliseconds. If the timeout is zero (the default value), it defaults to a reasonable timeout period as appropriate for the source.
See also start, stop, and active.
© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-6.2/qml-qtpositioning-positionsource.html