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
placesearchreply_esri.cpp
Go to the documentation of this file.
1// Copyright (C) 2013-2018 Esri <contracts@esri.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
7#include <QtCore/QJsonDocument>
8#include <QtCore/QJsonArray>
9#include <QtCore/QJsonObject>
10#include <QtNetwork/QNetworkReply>
11#include <QtPositioning/QGeoAddress>
12#include <QtPositioning/QGeoCircle>
13#include <QtPositioning/QGeoLocation>
14#include <QtPositioning/QGeoRectangle>
15#include <QtLocation/QPlace>
16#include <QtLocation/QPlaceAttribute>
17#include <QtLocation/QPlaceContactDetail>
18#include <QtLocation/QPlaceRatings>
19#include <QtLocation/QPlaceResult>
20#include <QtLocation/QPlaceSearchRequest>
21#include <QtLocation/private/qplacesearchrequest_p.h>
22
23static const QString kCandidatesKey(QStringLiteral("candidates"));
24static const QString kAttributesKey(QStringLiteral("attributes"));
25static const QString kAddressKey(QStringLiteral("address"));
26static const QString kLongLabelKey(QStringLiteral("LongLabel"));
27static const QString kCityKey(QStringLiteral("City"));
28static const QString kCountryKey(QStringLiteral("Country"));
29static const QString kRegionKey(QStringLiteral("Region"));
30static const QString kPostalKey(QStringLiteral("Postal"));
31static const QString kStAddrKey(QStringLiteral("StAddr"));
32static const QString kStateKey(QStringLiteral("State"));
33static const QString kDistrictKey(QStringLiteral("District"));
34static const QString kLocationKey(QStringLiteral("location"));
35static const QString kXKey(QStringLiteral("x"));
36static const QString kYKey(QStringLiteral("y"));
37static const QString kDistanceKey(QStringLiteral("Distance"));
38static const QString kPhoneKey(QStringLiteral("Phone"));
39static const QString kExtentKey(QStringLiteral("extent"));
40static const QString kXminKey(QStringLiteral("xmin"));
41static const QString kYminKey(QStringLiteral("ymin"));
42static const QString kXmaxKey(QStringLiteral("xmax"));
43static const QString kYmaxKey(QStringLiteral("ymax"));
44
46
48 const QHash<QString, QString> &candidateFields,
49 const QHash<QString, QString> &countries, PlaceManagerEngineEsri *parent) :
50 QPlaceSearchReply(parent), m_candidateFields(candidateFields), m_countries(countries)
51{
53 if (!reply) {
54 setError(UnknownError, QStringLiteral("Null reply"));
55 return;
56 }
58
59 connect(reply, &QNetworkReply::finished, this, &PlaceSearchReplyEsri::replyFinished);
60 connect(reply, &QNetworkReply::errorOccurred, this, &PlaceSearchReplyEsri::networkError);
63}
64
68
69void PlaceSearchReplyEsri::setError(QPlaceReply::Error errorCode, const QString &errorString)
70{
72 emit errorOccurred(errorCode, errorString);
73 setFinished(true);
74 emit finished();
75}
76
77void PlaceSearchReplyEsri::replyFinished()
78{
79 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
81
83 return;
84
86 if (!document.isObject()) {
87 setError(ParseError, tr("Response parse error"));
88 return;
89 }
90
91 QJsonValue suggestions = document.object().value(kCandidatesKey);
92 if (!suggestions.isArray()) {
93 setError(ParseError, tr("Response parse error"));
94 return;
95 }
96
97 const QJsonArray resultsArray = suggestions.toArray();
98
99 QList<QPlaceSearchResult> results;
100 for (const auto result : resultsArray)
101 results.append(parsePlaceResult(result.toObject()));
102
104 setFinished(true);
105 emit finished();
106}
107
108void PlaceSearchReplyEsri::networkError(QNetworkReply::NetworkError error)
109{
111 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
114}
115
116QPlaceResult PlaceSearchReplyEsri::parsePlaceResult(const QJsonObject &item) const
117{
118 QPlace place;
119 QHash<QString, QString> keys;
120
121 // set attributes
122 const QJsonObject attributes = item.value(kAttributesKey).toObject();
123 for (const QString &key: attributes.keys())
124 {
125 const QString value = attributes.value(key).toVariant().toString();
126 if (!value.isEmpty())
127 {
129 attribute.setLabel(m_candidateFields.value(key, key)); // local name or key
130 attribute.setText(value);
132 keys.insert(key, value);
133 }
134 }
135
136 if (keys.contains(kPhoneKey))
137 {
138 QPlaceContactDetail contactDetail;
139 contactDetail.setLabel(m_candidateFields.value(kPhoneKey, kPhoneKey)); // local name or key
140 contactDetail.setValue(keys.value(kPhoneKey));
142 }
143
144 // set address
145 QGeoAddress geoAddress;
146 geoAddress.setCity(keys.value(kCityKey));
147 geoAddress.setCountry(m_countries.value(keys.value(kCountryKey))); // mismatch code ISO2 vs ISO3
148 geoAddress.setCounty(keys.value(kRegionKey));
149 geoAddress.setPostalCode(keys.value(kPostalKey));
150 geoAddress.setStreet(keys.value(kStAddrKey));
151 geoAddress.setState(keys.value(kStateKey));
152 geoAddress.setDistrict(keys.value(kDistrictKey));
153
154 // set location
155 const QJsonObject location = item.value(kLocationKey).toObject();
156 const QGeoCoordinate coordinate = QGeoCoordinate(location.value(kYKey).toDouble(),
157 location.value(kXKey).toDouble());
158
159 // set boundingBox
160 const QJsonObject extent = item.value(kExtentKey).toObject();
161 const QGeoCoordinate topLeft(extent.value(kYminKey).toDouble(),
162 extent.value(kXminKey).toDouble());
163 const QGeoCoordinate bottomRight(extent.value(kYmaxKey).toDouble(),
164 extent.value(kXmaxKey).toDouble());
165 const QGeoRectangle boundingBox(topLeft, bottomRight);
166
167 // set geolocation
168 QGeoLocation geoLocation;
169 geoLocation.setCoordinate(coordinate);
170 geoLocation.setAddress(geoAddress);
171 geoLocation.setBoundingShape(boundingBox);
172
173 // set place
174 place.setName(keys.value(kLongLabelKey));
175 place.setLocation(geoLocation);
176 place.setPlaceId(attributes.value(kLongLabelKey).toString());
177
178 // set place result
180 result.setPlace(place);
181 result.setTitle(keys.value(kAddressKey));
182 result.setDistance(keys.value(kDistanceKey).toDouble());
183
184 return result;
185}
186
PlaceSearchReplyEsri(const QPlaceSearchRequest &request, QNetworkReply *reply, const QHash< QString, QString > &candidateFields, const QHash< QString, QString > &countries, PlaceManagerEngineEsri *parent)
\inmodule QtPositioning
Definition qgeoaddress.h:18
void setCity(const QString &city)
Sets the city.
\inmodule QtPositioning
\inmodule QtPositioning
void setCoordinate(const QGeoCoordinate &position)
Sets the coordinate of the location.
\inmodule QtPositioning
T value(const Key &key) const noexcept
Definition qhash.h:1054
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
QString errorString() const
Returns a human-readable description of the last device error that occurred.
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
\inmodule QtCore\reentrant
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error=nullptr)
Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
QJsonValue value(const QString &key) const
Returns a QJsonValue representing the value for the key key.
\inmodule QtCore\reentrant
Definition qjsonvalue.h:25
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.
QVariant toVariant() const
Converts the value to a \l {QVariant::}{QVariant()}.
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
void errorOccurred(QNetworkReply::NetworkError)
NetworkError error() const
Returns the error that was found during the processing of this request.
virtual void abort()=0
Aborts the operation immediately and close down any network connections still open.
NetworkError
Indicates all possible error conditions found during the processing of the request.
void finished()
This signal is emitted when the reply has finished processing.
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition qobject.cpp:2658
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
\inmodule QtLocation
The QPlaceContactDetail class represents a contact detail such as a phone number or website url.
void setLabel(const QString &label)
static const QString Phone
\qmlvaluetype contactDetail \inqmlmodule QtLocation
QPlaceReply::Error error() const
Returns the error code.
void errorOccurred(QPlaceReply::Error error, const QString &errorString=QString())
This signal is emitted when an error has been detected in the processing of this reply.
void finished()
This signal is emitted when this reply has finished processing.
Error
Describes an error which occurred during an operation.
Definition qplacereply.h:18
void aborted()
QString errorString() const
Returns the error string of the reply.
void setError(QPlaceReply::Error error, const QString &errorString)
Sets the error and errorString of the reply.
void setFinished(bool finished)
Sets the status of whether the reply is finished or not.
\inmodule QtLocation
void setPlace(const QPlace &place)
Sets the place that this result refers to.
\inmodule QtLocation
void setRequest(const QPlaceSearchRequest &request)
Sets the search request used to generate this reply.
QPlaceSearchRequest request() const
Returns the search request that was used to generate this reply.
QList< QPlaceSearchResult > results() const
Returns a list of search results;.
void setResults(const QList< QPlaceSearchResult > &results)
Sets the list of search results.
\inmodule QtLocation
\inmodule QtLocation
Definition qplace.h:25
void setPlaceId(const QString &identifier)
Sets the identifier of the place.
Definition qplace.cpp:314
void appendContactDetail(const QString &contactType, const QPlaceContactDetail &detail)
Appends a contact detail of a specified contactType.
Definition qplace.cpp:508
void setLocation(const QGeoLocation &location)
Sets the location of the place.
Definition qplace.cpp:197
void setExtendedAttribute(const QString &attributeType, const QPlaceAttribute &attribute)
Assigns an attribute of the given attributeType to a place.
Definition qplace.cpp:448
void setName(const QString &name)
Sets the name of the place.
Definition qplace.cpp:296
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
list append(new Employee("Blackpool", "Stephen"))
Combined button and popup list for selecting options.
emscripten::val document()
Definition qwasmdom.h:49
static const QString kLocationKey(QStringLiteral("location"))
static const QString kXKey(QStringLiteral("x"))
static const QString kYminKey(QStringLiteral("ymin"))
static const QString kExtentKey(QStringLiteral("extent"))
static const QString kStAddrKey(QStringLiteral("StAddr"))
static const QString kAttributesKey(QStringLiteral("attributes"))
static const QString kDistrictKey(QStringLiteral("District"))
static const QString kPhoneKey(QStringLiteral("Phone"))
static const QString kYmaxKey(QStringLiteral("ymax"))
static const QString kXmaxKey(QStringLiteral("xmax"))
static const QString kAddressKey(QStringLiteral("address"))
static const QString kDistanceKey(QStringLiteral("Distance"))
static const QString kStateKey(QStringLiteral("State"))
static const QString kPostalKey(QStringLiteral("Postal"))
static const QString kXminKey(QStringLiteral("xmin"))
static const QString kLongLabelKey(QStringLiteral("LongLabel"))
static const QString kCountryKey(QStringLiteral("Country"))
static const QString kYKey(QStringLiteral("y"))
static const QString kRegionKey(QStringLiteral("Region"))
static const QString kLocationKey(QStringLiteral("location"))
static const QString kCandidatesKey(QStringLiteral("candidates"))
static const QString kCityKey(QStringLiteral("City"))
DBusConnection const char DBusError * error
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
EGLOutputLayerEXT EGLint attribute
GLint location
GLuint64 key
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QStringLiteral(str)
#define tr(X)
#define emit
#define Q_UNUSED(x)
QStringList keys
QGraphicsItem * item
QNetworkRequest request(url)
QNetworkReply * reply