Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qmapboxcommon.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 Mapbox, Inc.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qmapboxcommon.h"
5
6#include <QtCore/QJsonArray>
7#include <QtPositioning/QGeoAddress>
8#include <QtPositioning/QGeoCoordinate>
9#include <QtPositioning/QGeoRectangle>
10
12{
13 if (category.isEmpty())
14 return category;
15
16 QString categoryName = category;
17 categoryName[0] = categoryName[0].toUpper();
18 return categoryName;
19}
20
21// https://www.mapbox.com/api-documentation/#response-object
23{
25
27
28 QString streetAddress = response.value(QStringLiteral("address")).toString();
29
30 // If place type is 'address', the street address is a combo of 'text' and
31 // 'address'. The former provides the street name, and the latter provides
32 // the street number in that case.
33 if (response.value(QStringLiteral("place_type")).isArray()) {
34 for (const QJsonValueConstRef value : response.value(QStringLiteral("place_type")).toArray()) {
35 if (!value.isString())
36 continue;
37 if (value.toString() == QStringLiteral("address")) {
38 streetAddress.prepend(response.value(QStringLiteral("text")).toString() + QLatin1Char(' '));
39 break;
40 }
41 }
42 }
43
44 if (response.value(QStringLiteral("properties")).isObject()) {
45 const QJsonObject properties = response.value(QStringLiteral("properties")).toObject();
46
47 // Prefer properties.address over address.
48 const QString addressString = properties.value(QStringLiteral("address")).toString();
49 if (!addressString.isEmpty())
50 streetAddress = addressString;
51 }
52
53 address.setStreet(streetAddress);
54
55 if (response.value(QStringLiteral("context")).isArray()) {
56 for (const QJsonValueConstRef value : response.value(QStringLiteral("context")).toArray()) {
57 if (!value.isObject())
58 continue;
59
60 const QJsonObject object = value.toObject();
61 const QString valueId = object.value(QStringLiteral("id")).toString();
62 const QString valueText = object.value(QStringLiteral("text")).toString();
63
64 if (valueId.isEmpty() || valueText.isEmpty())
65 continue;
66
67 // XXX: locality, neighborhood, address, poi, poi.landmark
68 if (valueId.startsWith(QStringLiteral("country"))) {
69 address.setCountry(valueText);
70 const QString countryCode = object.value(QStringLiteral("short_code")).toString();
71 if (!countryCode.isEmpty())
72 address.setCountryCode(countryCode);
73 } else if (valueId.startsWith(QStringLiteral("region"))) {
74 address.setState(valueText);
75 } else if (valueId.startsWith(QStringLiteral("postcode"))) {
76 address.setPostalCode(valueText);
77 } else if (valueId.startsWith(QStringLiteral("district"))) {
78 address.setDistrict(valueText);
79 } else if (valueId.startsWith(QStringLiteral("place"))) {
80 address.setCity(valueText);
81 }
82 }
83 } else {
84 // Fallback to using information from place_name.
85 const QString placeName = response.value(QStringLiteral("place_name")).toString();
86
87 // Remove actual place name.
88 address.setText(placeName.mid(placeName.indexOf(QLatin1Char(',')) + 1));
89 }
90
91 location.setAddress(address);
92
93 QJsonArray bbox = response.value(QStringLiteral("bbox")).toArray();
94 double top = bbox.at(3).toDouble();
95 double left = bbox.at(0).toDouble();
96 double bottom = bbox.at(1).toDouble();
97 double right = bbox.at(2).toDouble();
99
100 QJsonArray center = response.value(QStringLiteral("center")).toArray();
101 location.setCoordinate(QGeoCoordinate(center.at(1).toDouble(), center.at(0).toDouble()));
102
103 return location;
104}
\inmodule QtPositioning
Definition qgeoaddress.h:18
\inmodule QtPositioning
\inmodule QtPositioning
\inmodule QtPositioning
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
QJsonValue value(const QString &key) const
Returns a QJsonValue representing the value for the key key.
QJsonArray toArray() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
double toDouble(double defaultValue=0) const
Converts the value to a double and returns it.
bool isArray() const
Returns true if the value contains an array.
Definition qjsonvalue.h:76
QString toString() const
Converts the value to a QString and returns it.
bool isObject() const
Returns true if the value contains an object.
Definition qjsonvalue.h:77
static QString mapboxNameForCategory(const QString &category)
static QGeoLocation parseGeoLocation(const QJsonObject &response)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QString & prepend(QChar c)
Definition qstring.h:478
QString toUpper() const &
Definition qstring.h:439
const QLoggingCategory & category()
[1]
static const QCssKnownValue properties[NumProperties - 1]
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLint location
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLint left
GLint GLint bottom
GLuint GLuint64EXT address
#define QStringLiteral(str)
char * toString(const MyType &t)
[31]
\inmodule QtCore \reentrant
Definition qchar.h:18