6#include <QJsonDocument>
9#include <QGeoCoordinate>
11#include <QGeoLocation>
12#include <QGeoRectangle>
21 setError(UnknownError, QStringLiteral(
"Null reply"));
24 connect(reply, &QNetworkReply::finished,
this, &GeoCodeReplyEsri::networkReplyFinished);
25 connect(reply, &QNetworkReply::errorOccurred,
26 this, &GeoCodeReplyEsri::networkReplyError);
27 connect(
this, &QGeoCodeReply::aborted, reply, &QNetworkReply::abort);
28 connect(
this, &QObject::destroyed, reply, &QObject::deleteLater);
41 QNetworkReply *reply =
static_cast<QNetworkReply *>(sender());
43 setError(QGeoCodeReply::CommunicationError, reply->errorString());
48 QNetworkReply *reply =
static_cast<QNetworkReply *>(sender());
51 if (reply->error() != QNetworkReply::NoError)
54 QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
56 if (document.isObject()) {
57 QJsonObject object = document.object();
59 switch (operationType()) {
62 const QJsonArray candidates = object.value(QStringLiteral(
"candidates")).toArray();
64 QList<QGeoLocation> locations;
66 for (
const auto candidate : candidates) {
67 if (!candidate.isObject())
69 locations.append(parseCandidate(candidate.toObject()));
72 setLocations(locations);
79 setLocations({parseAddress(object)});
86 setError(QGeoCodeReply::CommunicationError, QStringLiteral(
"Unknown document"));
92 QJsonObject addressObject = object.value(QStringLiteral(
"address")).toObject();
96 address.setCountryCode(addressObject.value(QStringLiteral(
"CountryCode")).toString());
97 address.setState(addressObject.value(QStringLiteral(
"Region")).toString());
98 address.setCity(addressObject.value(QStringLiteral(
"City")).toString());
99 address.setDistrict(addressObject.value(QStringLiteral(
"Subregion")).toString());
100 address.setPostalCode(addressObject.value(QStringLiteral(
"Postal")).toString());
101 address.setStreet(addressObject.value(QStringLiteral(
"Address")).toString());
103 QGeoCoordinate coordinate;
105 QJsonObject locationObject = object.value(QStringLiteral(
"location")).toObject();
107 coordinate.setLongitude(locationObject.value(QStringLiteral(
"x")).toDouble());
108 coordinate.setLatitude(locationObject.value(QStringLiteral(
"y")).toDouble());
110 QGeoLocation location;
112 location.setCoordinate(coordinate);
113 location.setAddress(address);
120 QGeoCoordinate coordinate;
122 QJsonObject locationObject = candidate.value(QStringLiteral(
"location")).toObject();
124 coordinate.setLongitude(locationObject.value(QStringLiteral(
"x")).toDouble());
125 coordinate.setLatitude(locationObject.value(QStringLiteral(
"y")).toDouble());
127 QGeoRectangle extent;
129 if (candidate.contains(QStringLiteral(
"extent"))) {
130 QJsonObject extentObject = candidate.value(QStringLiteral(
"extent")).toObject();
132 extent.setTopLeft(QGeoCoordinate(extentObject.value(QStringLiteral(
"ymin")).toDouble(),
133 extentObject.value(QStringLiteral(
"xmin")).toDouble()));
135 extent.setBottomRight(QGeoCoordinate(extentObject.value(QStringLiteral(
"ymax")).toDouble(),
136 extentObject.value(QStringLiteral(
"xmax")).toDouble()));
139 QJsonObject attributesObject = candidate.value(QStringLiteral(
"attributes")).toObject();
143 address.setText(candidate.value(QStringLiteral(
"address")).toString());
145 address.setCountry(attributesObject.value(QStringLiteral(
"Country")).toString());
146 address.setCountryCode(attributesObject.value(QStringLiteral(
"Country")).toString());
147 address.setState(attributesObject.value(QStringLiteral(
"Region")).toString());
148 address.setCity(attributesObject.value(QStringLiteral(
"City")).toString());
149 address.setDistrict(attributesObject.value(QStringLiteral(
"Subregion")).toString());
150 address.setPostalCode(attributesObject.value(QStringLiteral(
"Postal")).toString());
152 QGeoLocation location;
154 location.setCoordinate(coordinate);
155 location.setBoundingShape(extent);
156 location.setAddress(address);
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
Combined button and popup list for selecting options.