Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
positioningplugin.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <private/qpositioningquickglobal_p.h>
6#include <QGeoCircle>
7#include <QGeoCoordinate>
8#include <QGeoPath>
9#include <QGeoPolygon>
10#include <QGeoRectangle>
11#include <QtPositioningQuick/private/qquickgeocoordinateanimation_p.h>
12#include <QtCore/QVariantAnimation>
13#include <QtQml/QQmlEngineExtensionPlugin>
14
15QT_BEGIN_NAMESPACE
16
17/*!
18 \qmlvaluetype geoCoordinate
19 \ingroup qmlvaluetypes
20 \inqmlmodule QtPositioning
21 \since 5.2
22
23 \brief The geoCoordinate type represents and stores a geographic position.
24
25 This type is a QML representation of \l QGeoCoordinate and represents a geographic
26 position in the form of \l {latitude}, \l longitude and \l altitude attributes.
27 The \l latitude attribute specifies the number of
28 decimal degrees above and below the equator. A positive latitude indicates the Northern
29 Hemisphere and a negative latitude indicates the Southern Hemisphere. The \l longitude
30 attribute specifies the number of decimal degrees east and west. A positive longitude
31 indicates the Eastern Hemisphere and a negative longitude indicates the Western Hemisphere.
32 The \l altitude attribute specifies the altitude in meters, and the \l altitudeType
33 attribute specifies the reference surface from which the altitude is measured, if it is
34 known. Together, these attributes specify a 3-dimensional position anywhere on or near
35 the Earth's surface.
36
37 The \l isValid attribute can be used to test if a coordinate is valid. A coordinate is
38 considered valid if it has a valid latitude and longitude. A valid altitude is not required.
39 The latitude must be between -90 and 90 inclusive and the longitude must be between -180 and
40 180 inclusive.
41
42 The geoCoordinate type is used by many other types in the Qt Location module, for specifying
43 the position of an object on a Map, the current position of a device and many other tasks.
44 They also feature a number of important utility methods that make otherwise complex
45 calculations simple to use, such as \l {atDistanceAndAzimuth}().
46
47 \section1 Accuracy
48
49 The latitude, longitude and altitude attributes stored in the geoCoordinate type are represented
50 as doubles, giving them approximately 16 decimal digits of precision -- enough to specify
51 micrometers. The calculations performed in geoCoordinate's methods such as \l {azimuthTo}() and
52 \l {distanceTo}() also use doubles for all intermediate values, but the inherent inaccuracies in
53 their spherical Earth model dominate the amount of error in their output.
54
55 \section1 Example Usage
56
57 To create a geoCoordinate use one of the methods described below. In all cases, specifying the
58 \l altitude attribute is optional.
59
60 To create a geoCoordinate value, use the \l{QtPositioning::coordinate}{QtPositioning.coordinate()}
61 function:
62
63 \qml
64 import QtPositioning
65
66 Location { coordinate: QtPositioning.coordinate(-27.5, 153.1) }
67 \endqml
68
69 or as separate \l latitude, \l longitude and \l altitude components:
70
71 \qml
72 Location {
73 coordinate {
74 latitude: -27.5
75 longitude: 153.1
76 }
77 }
78 \endqml
79
80 When integrating with C++, note that any QGeoCoordinate value passed into QML from C++ is
81 automatically converted into a geoCoordinate value, and vice-versa.
82*/
83
84/*!
85 \qmlproperty real geoCoordinate::latitude
86
87 This property holds the latitude value of the geographical position
88 (decimal degrees). A positive latitude indicates the Northern Hemisphere,
89 and a negative latitude indicates the Southern Hemisphere.
90 If the property has not been set, its default value is NaN.
91
92 For more details see the \l {QGeoCoordinate::latitude} property
93*/
94
95/*!
96 \qmlproperty real geoCoordinate::longitude
97
98 This property holds the longitude value of the geographical position
99 (decimal degrees). A positive longitude indicates the Eastern Hemisphere,
100 and a negative longitude indicates the Western Hemisphere
101 If the property has not been set, its default value is NaN.
102
103 For more details see the \l {QGeoCoordinate::longitude} property
104*/
105
106/*!
107 \qmlproperty real geoCoordinate::altitude
108
109 This property holds the altitude value (in meters).
110 If the property has not been set, its default value is NaN.
111
112 Use the \l altitudeType property to determine the reference surface from
113 which the altitude is measured.
114
115 See \l {QGeoCoordinate::altitude()} for more details.
116*/
117
118/*!
119 \qmlproperty enumeration geoCoordinate::altitudeType
120 \since 6.13
121
122 This property holds the reference surface from which the \l altitude is
123 measured.
124
125 \value GeoCoordinate.Unknown The reference surface of the altitude is not
126 known. This is the default value.
127 \value GeoCoordinate.MSL The altitude is measured above the mean sea level
128 (MSL).
129 \value GeoCoordinate.WGS84 The altitude is measured above the WGS84
130 reference ellipsoid.
131
132 Setting this property does not convert the \l altitude value. It only
133 changes the reference surface that the current altitude value is assumed
134 to be measured from.
135
136 For more details see the \l {QGeoCoordinate::altitudeType} property and
137 the \l {QGeoCoordinate::AltitudeType} enum.
138*/
139
140/*!
141 \qmlproperty bool geoCoordinate::isValid
142
143 This property holds the current validity of the coordinate. Coordinates
144 are considered valid if they have been set with a valid latitude and
145 longitude (altitude is not required).
146
147 The latitude must be between -90 to 90 inclusive to be considered valid,
148 and the longitude must be between -180 to 180 inclusive to be considered
149 valid.
150
151 This is a read-only property.
152*/
153
154/*!
155 \qmlmethod real geoCoordinate::distanceTo(geoCoordinate other)
156
157 Returns the distance (in meters) from this coordinate to the coordinate specified by \a other.
158 Altitude is not used in the calculation.
159
160 This calculation returns the great-circle distance between the two coordinates, with an
161 assumption that the Earth is spherical for the purpose of this calculation.
162*/
163
164/*!
165 \qmlmethod real geoCoordinate::azimuthTo(geoCoordinate other)
166
167 Returns the azimuth (or bearing) in degrees from this coordinate to the coordinate specified by
168 \a other. Altitude is not used in the calculation.
169
170 There is an assumption that the Earth is spherical for the purpose of this calculation.
171*/
172
173/*!
174 \qmlmethod geoCoordinate geoCoordinate::atDistanceAndAzimuth(real distance, real azimuth)
175
176 Returns the coordinate that is reached by traveling \a distance metres from this coordinate at
177 \a azimuth degrees along a great-circle.
178
179 There is an assumption that the Earth is spherical for the purpose of this calculation.
180*/
181
182/*!
183 \qmlvaluetype geoShape
184 \ingroup qmlvaluetypes
185 \inqmlmodule QtPositioning
186 \since 5.2
187
188 \brief A geoShape type represents an abstract geographic area.
189
190 This type is a QML representation of \l QGeoShape which is an abstract geographic area.
191 It includes attributes and methods common to all geographic areas. To create objects
192 that represent a valid geographic area use \l {geoRectangle} or \l {geoCircle}.
193
194 The \l isValid attribute can be used to test if the shape represents a valid geographic
195 area.
196
197 The \l isEmpty attribute can be used to test if the shape represents a region with a
198 geometrical area of 0.
199
200 The \l {contains}() method can be used to test if a \l geoCoordinate is
201 within the shape.
202
203 \section1 Example Usage
204
205 To create a geoShape value, use the
206 \l {QtPositioning::shape}{QtPositioning.shape()} function:
207
208 \qml
209 import QtPositioning
210
211 Item {
212 property geoShape region: QtPositioning.shape()
213 }
214 \endqml
215
216 When integrating with C++, note that any QGeoShape value passed into QML from C++ is
217 automatically converted into a geoShape value, and vice-versa.
218*/
219
220/*!
221 \qmlproperty bool geoShape::isEmpty
222
223 Returns whether this shape is empty. An empty shape is a region which has
224 a geometrical area of 0.
225*/
226
227/*!
228 \qmlproperty bool geoShape::isValid
229
230 Returns whether this shape is valid.
231
232 A shape is considered to be invalid if some of the data that is required to
233 unambiguously describe the shape has not been set or has been set to an
234 unsuitable value.
235*/
236
237/*!
238 \qmlproperty ShapeType geoShape::type
239
240 Returns the current type of the shape.
241
242 \list
243 \li \c GeoShape.UnknownType - The shape's type is not known.
244 \li \c GeoShape.RectangleType - The shape is a \l geoRectangle.
245 \li \c GeoShape.CircleType - The shape is a \l geoCircle.
246 \li \c GeoShape.PathType - The shape is a \l geoPath. (Since Qt 5.9)
247 \li \c GeoShape.PolygonType - The shape is a \l geoPolygon. (Since Qt 5.10)
248 \endlist
249
250 This QML property was introduced by Qt 5.5.
251*/
252
253/*!
254 \qmlmethod bool geoShape::contains(geoCoordinate coord)
255
256 Returns true if the \l {QtPositioning::geoCoordinate}{coordinate} specified by \a coord is within
257 this shape; otherwise returns false.
258*/
259
260/*!
261 \qmlvaluetype geoRectangle
262 \ingroup qmlvaluetypes
263 \inqmlmodule QtPositioning
264 \since 5.2
265
266 \brief The geoRectangle type represents a rectangular geographic area.
267
268 The geoRectangle type is a \l {geoShape} that represents a
269 rectangular geographic area. The type is direct representation of a \l QGeoRectangle.
270 It is defined by a pair of \l {geoCoordinate}{coordinates} which represent the top-left
271 and bottom-right corners of the rectangle. The coordinates are accessible
272 from the \l topLeft and \l bottomRight attributes.
273
274 A rectangle is considered invalid if the top-left or bottom-right coordinates are invalid
275 or if the top-left coordinate is south of the bottom-right coordinate.
276
277 The coordinates of the four corners of the rectangle can be accessed with the
278 \l {topLeft}, \l {topRight}, \l {bottomLeft} and \l {bottomRight} attributes. The \l center
279 attribute can be used to get the coordinate of the center of the rectangle. The \l width
280 and \l height attributes can be used to get the width and height of the rectangle in
281 degrees. Setting one of these attributes will cause the other attributes to be adjusted
282 accordingly.
283
284 \section1 Limitations
285
286 A geoRectangle can never cross the poles.
287
288 If the height or center of a geoRectangle is adjusted such that it would cross one of the
289 poles the height is modified such that the rectangle touches but does not cross the pole
290 and that the center coordinate is still in the center of the rectangle.
291
292 \section1 Example Usage
293
294 To create a geoRectangle value, use the \l {QtPositioning::rectangle}{QtPositioning.rectangle()}
295 function:
296
297 \qml
298 import QtPositioning
299
300 Item {
301 property geoRectangle region: QtPositioning.rectangle(QtPositioning.coordinate(-27.5, 153.1),
302 QtPositioning.coordinate(-27.6, 153.2))
303 }
304 \endqml
305
306 When integrating with C++, note that any QGeoRectangle value passed into QML from C++ is
307 automatically converted into a geoRectangle value, and vice-versa.
308*/
309
310/*!
311 \qmlproperty geoCoordinate geoRectangle::bottomLeft
312
313 This property holds the bottom left coordinate of this geoRectangle.
314*/
315
316/*!
317 \qmlproperty geoCoordinate geoRectangle::bottomRight
318
319 This property holds the bottom right coordinate of this geoRectangle.
320*/
321
322/*!
323 \qmlproperty geoCoordinate geoRectangle::center
324
325 This property holds the center coordinate of this geoRectangle. For more details
326 see \l {QGeoRectangle::setCenter()}.
327*/
328
329/*!
330 \qmlproperty double geoRectangle::height
331
332 This property holds the height of this geoRectangle (in degrees). For more details
333 see \l {QGeoRectangle::setHeight()}.
334
335 \note If the geoRectangle is invalid, it is not possible to set the height. QtPositioning
336 releases prior to Qt 5.5 permitted the setting of the height even on invalid geoRectangles.
337*/
338
339/*!
340 \qmlproperty geoCoordinate geoRectangle::topLeft
341
342 This property holds the top left coordinate of this geoRectangle.
343*/
344
345/*!
346 \qmlproperty geoCoordinate geoRectangle::topRight
347
348 This property holds the top right coordinate of this geoRectangle.
349*/
350
351/*!
352 \qmlproperty double geoRectangle::width
353
354 This property holds the width of this geoRectangle (in degrees). For more details
355 see \l {QGeoRectangle::setWidth()}.
356
357 \note If the geoRectangle is invalid, it is not possible to set the width. QtPositioning
358 releases prior to Qt 5.5 permitted the setting of the width even on invalid geoRectangles.
359*/
360
361/*!
362 \qmlvaluetype geoCircle
363 \ingroup qmlvaluetypes
364 \inqmlmodule QtPositioning
365 \since 5.2
366
367 \brief The geoCircle type represents a circular geographic area.
368
369 The geoCircle type is a \l {geoShape} that represents a circular
370 geographic area. It is a direct representation of a \l QGeoCircle and is defined
371 in terms of a \l {geoCoordinate}{coordinate} which specifies the \l center of the circle and
372 a qreal which specifies the \l radius of the circle in meters.
373
374 The circle is considered invalid if the \l center coordinate is invalid or if
375 the \l radius is less than zero.
376
377 \section1 Example Usage
378
379 To create a geoCircle value, use the \l {QtPositioning::circle}{QtPositioning.circle()}
380 function:
381
382 \qml
383 import QtPositioning
384
385 Item {
386 property geoCircle region: QtPositioning.circle(QtPositioning.coordinate(-27.5, 153.1), 1000)
387 }
388 \endqml
389
390 When integrating with C++, any QGeoCircle value passed into QML from C++ is
391 automatically converted into a geoCircle value. Similarly, geoCircle
392 values are converted to QGeoCircle values when passed from QML to C++.
393*/
394
395/*!
396 \qmlproperty geoCoordinate geoCircle::center
397
398 This property holds the coordinate of the center of the geoCircle.
399*/
400
401/*!
402 \qmlproperty real geoCircle::radius
403
404 This property holds the radius of the geoCircle in meters.
405
406 The default value for the radius is -1 indicating an invalid geoCircle area.
407*/
408
409/*!
410 \qmlvaluetype geoPath
411 \ingroup qmlvaluetypes
412 \inqmlmodule QtPositioning
413 \since 5.9
414
415 \brief The geoPath type represents a geographic path.
416
417 The geoPath type is a \l {geoShape} that represents a geographic
418 path. It is a direct representation of a \l QGeoPath and is defined
419 in terms of a \l {path} which holds the list of geoCoordinates in the
420 path.
421
422 The path is considered invalid if it is empty.
423
424 When integrating with C++, note that any QGeoPath value passed into QML from C++ is
425 automatically converted into a geoPath value, and vice versa.
426*/
427
428/*!
429 \qmlproperty list<geoCoordinate> geoPath::path
430
431 This property holds the list of coordinates defining the path.
432*/
433
434/*!
435 \qmlproperty real geoPath::width
436
437 This property holds the width of the path in meters. This is currently only used
438 when calling the \l {geoShape::}{contains()} method.
439
440 The default value for the width is 0.
441*/
442
443/*!
444 \qmlvaluetype geoPolygon
445 \ingroup qmlvaluetypes
446 \inqmlmodule QtPositioning
447 \since 5.10
448
449 \brief The geoPolygon type represents a geographic polygon.
450
451 The geoPolygon type is a \l [QML] geoShape that represents a geographic
452 polygon. It is a direct representation of QGeoPolygon and is defined in
453 terms of a \l path which holds a list of geoCoordinates in the polygon.
454
455 The polygon is considered invalid if its path holds less than three
456 coordinates.
457
458 When integrating with C++, note that any QGeoPolygon value passed into QML
459 is automatically converted into a geoPolygon, and vice versa.
460*/
461
462/*!
463 \qmlproperty list<geoCoordinate> geoPolygon::path
464
465 This property holds the list of coordinates defining the polygon.
466*/
467
468class QtPositioningDeclarativeModule: public QQmlEngineExtensionPlugin
469{
470 Q_OBJECT
471
472 Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
473
474public:
475 QtPositioningDeclarativeModule(QObject *parent = 0) : QQmlEngineExtensionPlugin(parent)
476 {
477 volatile auto registration = &qml_register_types_QtPositioning;
478 Q_UNUSED(registration)
479 }
480};
481
482namespace {
483
484bool parseCoordinate(const QVariantMap &map, QGeoCoordinate &c)
485{
486 if (const auto it = map.find(QStringLiteral("latitude")); it != map.end())
487 c.setLatitude(it.value().toDouble());
488 else
489 c.setLatitude(qQNaN());
490 if (const auto it = map.find(QStringLiteral("longitude")); it != map.end())
491 c.setLongitude(it.value().toDouble());
492 else
493 c.setLongitude(qQNaN());
494 if (const auto it = map.find(QStringLiteral("altitude")); it != map.end())
495 c.setAltitude(it.value().toDouble());
496 else
497 c.setAltitude(qQNaN());
498
499 // Not considering the case where the map is valid but containing NaNs.
500 return c.isValid();
501}
502
503bool parseRectangle(const QVariantMap &map, QGeoRectangle &rect)
504{
505 if (const auto it = map.find(QStringLiteral("topLeft")); it != map.end())
506 rect.setTopLeft(it.value().value<QGeoCoordinate>());
507 if (const auto it = map.find(QStringLiteral("bottomLeft")); it != map.end())
508 rect.setBottomLeft(it.value().value<QGeoCoordinate>());
509 if (const auto it = map.find(QStringLiteral("topRight")); it != map.end())
510 rect.setTopRight(it.value().value<QGeoCoordinate>());
511 if (const auto it = map.find(QStringLiteral("bottomRight")); it != map.end())
512 rect.setBottomRight(it.value().value<QGeoCoordinate>());
513 if (const auto it = map.find(QStringLiteral("center")); it != map.end())
514 rect.setCenter(it.value().value<QGeoCoordinate>());
515 if (const auto it = map.find(QStringLiteral("width")); it != map.end())
516 rect.setWidth(it.value().toDouble());
517 if (const auto it = map.find(QStringLiteral("height")); it != map.end())
518 rect.setHeight(it.value().toDouble());
519
520 // Not considering the case where the map is valid but containing NaNs.
521 return rect.isValid();
522}
523}
524
526{
527 QMetaType::registerConverter<QGeoRectangle, QGeoShape>();
528 QMetaType::registerConverter<QGeoShape, QGeoRectangle>();
529 QMetaType::registerConverter<QGeoShape, QGeoCircle>();
530 QMetaType::registerConverter<QGeoCircle, QGeoShape>();
531 QMetaType::registerConverter<QGeoShape, QGeoPath>();
532 QMetaType::registerConverter<QGeoPath, QGeoShape>();
533 QMetaType::registerConverter<QGeoShape, QGeoPolygon>();
534 QMetaType::registerConverter<QGeoPolygon, QGeoShape>();
535
536 if (!QMetaType::registerConverterFunction([](const void *src, void *target) -> bool {
537 const QVariantMap &map = *static_cast<const QVariantMap *>(src);
538 QGeoCoordinate &coord = *static_cast<QGeoCoordinate *>(target);
539 return parseCoordinate(map, coord);
540 }, QMetaType::fromType<QVariantMap>(), QMetaType::fromType<QGeoCoordinate>())) {
541 qWarning("Failed to register conversion function from QVariantMap to QGeoCoordinate");
542 }
543
544 if (!QMetaType::registerConverterFunction([](const void *src, void *target) -> bool {
545 const QVariantMap &map = *static_cast<const QVariantMap *>(src);
546 QGeoRectangle &rect = *static_cast<QGeoRectangle *>(target);
547 return parseRectangle(map, rect);
548 }, QMetaType::fromType<QVariantMap>(), QMetaType::fromType<QGeoRectangle>())) {
549 qWarning("Failed to register conversion function from QVariantMap to QGeoRectangle");
550 }
551
552 qRegisterAnimationInterpolator<QGeoCoordinate>(q_coordinateInterpolator);
553}
554
555Q_CONSTRUCTOR_FUNCTION(QtPositioningDeclarative_initializeModule)
556
557QT_END_NAMESPACE
558
559#include "positioningplugin.moc"
void QtPositioningDeclarative_initializeModule()