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>
23static const QString kCandidatesKey(QStringLiteral(
"candidates"));
47PlaceSearchReplyEsri::PlaceSearchReplyEsri(
const QPlaceSearchRequest &request, QNetworkReply *reply,
48 const QHash<QString, QString> &candidateFields,
49 const QHash<QString, QString> &countries, PlaceManagerEngineEsri *parent) :
50 QPlaceSearchReply(parent), m_candidateFields(candidateFields), m_countries(countries)
54 setError(UnknownError, QStringLiteral(
"Null reply"));
59 connect(reply, &QNetworkReply::finished,
this, &PlaceSearchReplyEsri::replyFinished);
60 connect(reply, &QNetworkReply::errorOccurred,
this, &PlaceSearchReplyEsri::networkError);
61 connect(
this, &QPlaceReply::aborted, reply, &QNetworkReply::abort);
62 connect(
this, &QObject::destroyed, reply, &QObject::deleteLater);
71 QPlaceReply::setError(errorCode, errorString);
72 emit errorOccurred(errorCode, errorString);
79 QNetworkReply *reply =
static_cast<QNetworkReply *>(sender());
82 if (reply->error() != QNetworkReply::NoError)
85 QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
86 if (!document.isObject()) {
87 setError(ParseError, tr(
"Response parse error"));
91 QJsonValue suggestions = document.object().value(kCandidatesKey);
92 if (!suggestions.isArray()) {
93 setError(ParseError, tr(
"Response parse error"));
97 const QJsonArray resultsArray = suggestions.toArray();
99 QList<QPlaceSearchResult> results;
100 for (
const auto result : resultsArray)
101 results.append(parsePlaceResult(result.toObject()));
111 QNetworkReply *reply =
static_cast<QNetworkReply *>(sender());
112 reply->deleteLater();
113 setError(QPlaceReply::CommunicationError, reply->errorString());
119 QHash<QString, QString> keys;
122 const QJsonObject attributes = item.value(kAttributesKey).toObject();
123 for (
const QString &key: attributes.keys())
125 const QString value = attributes.value(key).toVariant().toString();
126 if (!value.isEmpty())
128 QPlaceAttribute attribute;
129 attribute.setLabel(m_candidateFields.value(key, key));
130 attribute.setText(value);
131 place.setExtendedAttribute(key, attribute);
132 keys.insert(key, value);
136 if (keys.contains(kPhoneKey))
138 QPlaceContactDetail contactDetail;
139 contactDetail.setLabel(m_candidateFields.value(kPhoneKey, kPhoneKey));
140 contactDetail.setValue(keys.value(kPhoneKey));
141 place.appendContactDetail(QPlaceContactDetail::Phone, contactDetail);
145 QGeoAddress geoAddress;
146 geoAddress.setCity(keys.value(kCityKey));
147 geoAddress.setCountry(m_countries.value(keys.value(kCountryKey)));
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));
155 const QJsonObject location = item.value(kLocationKey).toObject();
156 const QGeoCoordinate coordinate = QGeoCoordinate(location.value(kYKey).toDouble(),
157 location.value(kXKey).toDouble());
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);
168 QGeoLocation geoLocation;
169 geoLocation.setCoordinate(coordinate);
170 geoLocation.setAddress(geoAddress);
171 geoLocation.setBoundingShape(boundingBox);
174 place.setName(keys.value(kLongLabelKey));
175 place.setLocation(geoLocation);
176 place.setPlaceId(attributes.value(kLongLabelKey).toString());
180 result.setPlace(place);
181 result.setTitle(keys.value(kAddressKey));
182 result.setDistance(keys.value(kDistanceKey).toDouble());
static const QString kPostalKey(QStringLiteral("Postal"))
static const QString kYmaxKey(QStringLiteral("ymax"))
static const QString kPhoneKey(QStringLiteral("Phone"))
static const QString kAddressKey(QStringLiteral("address"))
static const QString kCountryKey(QStringLiteral("Country"))
static const QString kDistanceKey(QStringLiteral("Distance"))
static const QString kStAddrKey(QStringLiteral("StAddr"))
static const QString kAttributesKey(QStringLiteral("attributes"))
static const QString kLongLabelKey(QStringLiteral("LongLabel"))
static const QString kXminKey(QStringLiteral("xmin"))
static const QString kCityKey(QStringLiteral("City"))
static const QString kLocationKey(QStringLiteral("location"))
static const QString kDistrictKey(QStringLiteral("District"))
static const QString kXKey(QStringLiteral("x"))
static const QString kYminKey(QStringLiteral("ymin"))
static const QString kExtentKey(QStringLiteral("extent"))
static const QString kYKey(QStringLiteral("y"))
static const QString kXmaxKey(QStringLiteral("xmax"))
static const QString kRegionKey(QStringLiteral("Region"))
static const QString kStateKey(QStringLiteral("State"))