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
locationsingleton.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
5#include <QtPositioning/private/qwebmercator_p.h>
6#include <QtPositioning/private/qdoublevector2d_p.h>
7#include <QDebug>
8
10
11static QGeoCoordinate parseCoordinate(const QJSValue &value, bool *ok)
12{
13 QGeoCoordinate c;
14
15 if (value.isObject()) {
16 if (value.hasProperty(QStringLiteral("latitude")))
17 c.setLatitude(value.property(QStringLiteral("latitude")).toNumber());
18 if (value.hasProperty(QStringLiteral("longitude")))
19 c.setLongitude(value.property(QStringLiteral("longitude")).toNumber());
20 if (value.hasProperty(QStringLiteral("altitude")))
21 c.setAltitude(value.property(QStringLiteral("altitude")).toNumber());
22
23 if (ok)
24 *ok = true;
25 } else if (ok) {
26 *ok = false;
27 }
28
29 return c;
30}
31
32
33/*!
34 \qmltype QtPositioning
35 \inqmlmodule QtPositioning
36 \since 5.2
37
38 \brief The QtPositioning global object provides useful functions for working with location-based
39 types in QML.
40
41 \qml
42 import QtPositioning
43
44 Item {
45 property var coordinate: QtPositioning.coordinate(-27.5, 153.1)
46 }
47 \endqml
48*/
49
50LocationSingleton::LocationSingleton(QObject *parent)
51: QObject(parent)
52{
53}
54
55/*!
56 \qmlmethod coordinate QtPositioning::coordinate()
57
58 Constructs an invalid coordinate.
59
60*/
61QGeoCoordinate LocationSingleton::coordinate() const
62{
63 return QGeoCoordinate();
64}
65
66/*!
67 \qmlmethod coordinate QtPositioning::coordinate(real latitude, real longitude, real altitude) const
68
69 Constructs a coordinate with the specified \a latitude, \a longitude and optional \a altitude.
70 Both \a latitude and \a longitude must be valid, otherwise an invalid coordinate is returned.
71
72 \sa {coordinate}
73*/
74QGeoCoordinate LocationSingleton::coordinate(double latitude, double longitude, double altitude) const
75{
76 return QGeoCoordinate(latitude, longitude, altitude);
77}
78
79/*!
80 \qmlmethod geoshape QtPositioning::shape() const
81
82 Constructs an invalid geoshape.
83
84 \sa {geoshape}
85*/
86QGeoShape LocationSingleton::shape() const
87{
88 return QGeoShape();
89}
90
91/*!
92 \qmlmethod georectangle QtPositioning::rectangle() const
93
94 Constructs an invalid georectangle.
95
96 \sa {georectangle}
97*/
98QGeoRectangle LocationSingleton::rectangle() const
99{
100 return QGeoRectangle();
101}
102
103/*!
104 \qmlmethod georectangle QtPositioning::rectangle(coordinate center, real width, real height) const
105
106 Constructs a georectangle centered at \a center with a width of \a width degrees and a hight of
107 \a height degrees.
108
109 \sa {georectangle}
110*/
111QGeoRectangle LocationSingleton::rectangle(const QGeoCoordinate &center,
112 double width, double height) const
113{
114 return QGeoRectangle(center, width, height);
115}
116
117/*!
118 \qmlmethod georectangle QtPositioning::rectangle(coordinate topLeft, coordinate bottomRight) const
119
120 Constructs a georectangle with its top left corner positioned at \a topLeft and its bottom
121 right corner positioned at \a {bottomRight}.
122
123 \sa {georectangle}
124*/
125QGeoRectangle LocationSingleton::rectangle(const QGeoCoordinate &topLeft,
126 const QGeoCoordinate &bottomRight) const
127{
128 return QGeoRectangle(topLeft, bottomRight);
129}
130
131/*!
132 \qmlmethod georectangle QtPositioning::rectangle(list<coordinate> coordinates) const
133
134 Constructs a georectangle from the list of coordinates, the returned list is the smallest possible
135 containing all the coordinates.
136
137 \sa {georectangle}
138*/
139QGeoRectangle LocationSingleton::rectangle(const QVariantList &coordinates) const
140{
141 QList<QGeoCoordinate> internalCoordinates;
142 for (const auto &coordinate : coordinates) {
143 if (coordinate.canConvert<QGeoCoordinate>())
144 internalCoordinates << coordinate.value<QGeoCoordinate>();
145 }
146
147 return QGeoRectangle(internalCoordinates);
148}
149
150/*!
151 \qmlmethod geocircle QtPositioning::circle() const
152
153 Constructs an invalid geocircle.
154
155 \sa {geocircle}
156*/
157QGeoCircle LocationSingleton::circle() const
158{
159 return QGeoCircle();
160}
161
162/*!
163 \qmlmethod geocircle QtPositioning::circle(coordinate center, real radius) const
164
165 Constructs a geocircle centered at \a center with a radius of \a radius meters.
166*/
167QGeoCircle LocationSingleton::circle(const QGeoCoordinate &center, qreal radius) const
168{
169 return QGeoCircle(center, radius);
170}
171
172/*!
173 \qmlmethod geopath QtPositioning::path() const
174
175 Constructs an empty geopath.
176
177 \sa {geopath}
178 \since 5.9
179*/
180QGeoPath LocationSingleton::path() const
181{
182 return QGeoPath();
183}
184
185/*!
186 \qmlmethod geopath QtPositioning::path(list<coordinate> coordinates, real width) const
187
188 Constructs a geopath from coordinates and width.
189
190 \sa {geopath}
191 \since 5.9
192*/
193QGeoPath LocationSingleton::path(const QJSValue &value, qreal width) const
194{
195 QList<QGeoCoordinate> pathList;
196
197 if (value.isArray()) {
198 quint32 length = value.property(QStringLiteral("length")).toUInt();
199 for (quint32 i = 0; i < length; ++i) {
200 bool ok = false;
201 QGeoCoordinate c = parseCoordinate(value.property(i), &ok);
202
203 if (!ok || !c.isValid()) {
204 pathList.clear(); // aborting
205 break;
206 }
207
208 pathList.append(c);
209 }
210 }
211
212 return QGeoPath(pathList, width);
213}
214
215/*!
216 \qmlmethod geopolygon QtPositioning::polygon() const
217
218 Constructs an empty polygon.
219
220 \sa {geopolygon}
221 \since 5.10
222*/
223QGeoPolygon LocationSingleton::polygon() const
224{
225 return QGeoPolygon();
226}
227
228/*!
229 \qmlmethod geopolygon QtPositioning::polygon(list<coordinate> coordinates) const
230
231 Constructs a polygon from coordinates.
232
233 \sa {geopolygon}
234 \since 5.10
235*/
236QGeoPolygon LocationSingleton::polygon(const QVariantList &coordinates) const
237{
238 QList<QGeoCoordinate> internalCoordinates;
239 for (const auto &coordinate : coordinates) {
240 if (coordinate.canConvert<QGeoCoordinate>())
241 internalCoordinates << coordinate.value<QGeoCoordinate>();
242 }
243
244 return QGeoPolygon(internalCoordinates);
245}
246
247/*!
248 \qmlmethod geopolygon QtPositioning::polygon(list<coordinate> perimeter, list<list<coordinate>> holes) const
249
250 Constructs a polygon from coordinates for perimeter and inner holes.
251
252 \sa {geopolygon}
253 \since 5.12
254*/
255QGeoPolygon LocationSingleton::polygon(const QVariantList &perimeter, const QVariantList &holes) const
256{
257 QList<QGeoCoordinate> internalCoordinates;
258 for (const auto &coordinate : perimeter) {
259 if (coordinate.canConvert<QGeoCoordinate>())
260 internalCoordinates << coordinate.value<QGeoCoordinate>();
261 }
262 QGeoPolygon poly(internalCoordinates);
263
264 for (const auto &h : holes) {
265 if (h.metaType().id() == QMetaType::QVariantList) {
266 QList<QGeoCoordinate> hole;
267 const QVariantList &holeData = h.toList();
268 for (const auto &coord : holeData) {
269 if (coord.canConvert<QGeoCoordinate>())
270 hole << coord.value<QGeoCoordinate>();
271 }
272 if (!hole.isEmpty())
273 poly.addHole(hole);
274 }
275 }
276
277 return poly;
278}
279
280/*!
281 \qmlmethod geocircle QtPositioning::shapeToCircle(geoshape shape) const
282
283 Converts \a shape to a geocircle.
284
285 \sa {geocircle}
286 \since 5.5
287*/
288QGeoCircle LocationSingleton::shapeToCircle(const QGeoShape &shape) const
289{
290 return QGeoCircle(shape);
291}
292
293/*!
294 \qmlmethod georectangle QtPositioning::shapeToRectangle(geoshape shape) const
295
296 Converts \a shape to a georectangle.
297
298 \sa {georectangle}
299 \since 5.5
300*/
301QGeoRectangle LocationSingleton::shapeToRectangle(const QGeoShape &shape) const
302{
303 return QGeoRectangle(shape);
304}
305
306/*!
307 \qmlmethod geopath QtPositioning::shapeToPath(geoshape shape) const
308
309 Converts \a shape to a geopath.
310
311 \sa {geopath}
312 \since 5.9
313*/
314QGeoPath LocationSingleton::shapeToPath(const QGeoShape &shape) const
315{
316 return QGeoPath(shape);
317}
318
319/*!
320 \qmlmethod geopolygon QtPositioning::shapeToPolygon(geoshape shape) const
321
322 Converts \a shape to a polygon.
323
324 \sa {geopolygon}
325 \since 5.10
326*/
327QGeoPolygon LocationSingleton::shapeToPolygon(const QGeoShape &shape) const
328{
329 return QGeoPolygon(shape);
330}
331
332/*!
333 \qmlmethod coordinate QtPositioning::mercatorToCoord(point mercator) const
334
335 Converts a \a mercator coordinate into a latitude-longitude coordinate.
336
337 \sa {coordToMercator}
338 \since 5.12
339*/
340QGeoCoordinate LocationSingleton::mercatorToCoord(const QPointF &mercator) const
341{
342 return QWebMercator::mercatorToCoord(QDoubleVector2D(mercator.x(), mercator.y()));
343}
344
345/*!
346 \qmlmethod point QtPositioning::coordToMercator(coordinate coord) const
347
348 Converts a coordinate \a coord into a mercator coordinate and returns it.
349 \sa {mercatorToCoord}
350 \since 5.12
351*/
352QPointF LocationSingleton::coordToMercator(const QGeoCoordinate &coord) const
353{
354 return QWebMercator::coordToMercator(coord).toPointF();
355}
356
357QT_END_NAMESPACE
358
359#include "moc_locationsingleton_p.cpp"
static QT_BEGIN_NAMESPACE QGeoCoordinate parseCoordinate(const QJSValue &value, bool *ok)
Combined button and popup list for selecting options.