6#include <QtCore/QJsonDocument>
7#include <QtCore/QJsonObject>
8#include <QtCore/QJsonArray>
9#include <QtPositioning/QGeoCoordinate>
10#include <QtPositioning/QGeoAddress>
11#include <QtPositioning/QGeoLocation>
12#include <QtPositioning/QGeoRectangle>
13#include <QtLocation/private/qgeojson_p.h>
21 setError(UnknownError, QStringLiteral(
"Null reply"));
24 connect(reply, &QNetworkReply::finished,
25 this, &QGeoCodeReplyOsm::networkReplyFinished);
26 connect(reply, &QNetworkReply::errorOccurred,
27 this, &QGeoCodeReplyOsm::networkReplyError);
28 connect(
this, &QGeoCodeReply::aborted, reply, &QNetworkReply::abort);
29 connect(
this, &QObject::destroyed, reply, &QObject::deleteLater);
34QGeoCodeReplyOsm::~QGeoCodeReplyOsm()
41 address.setText(object.value(QStringLiteral(
"display_name")).toString());
42 QJsonObject ao = object.value(QStringLiteral(
"address")).toObject();
44 address.setCountry(ao.value(QStringLiteral(
"country")).toString());
46 address.setCountryCode(ao.value(QStringLiteral(
"country_code")).toString());
48 address.setState(ao.value(QStringLiteral(
"state")).toString());
50 if (ao.contains(QLatin1String(
"city")))
51 address.setCity(ao.value(QStringLiteral(
"city")).toString());
52 else if (ao.contains(QLatin1String(
"town")))
53 address.setCity(ao.value(QLatin1String(
"town")).toString());
54 else if (ao.contains(QLatin1String(
"village")))
55 address.setCity(ao.value(QLatin1String(
"village")).toString());
57 address.setCity(ao.value(QLatin1String(
"hamlet")).toString());
59 address.setDistrict(ao.value(QStringLiteral(
"suburb")).toString());
61 address.setPostalCode(ao.value(QStringLiteral(
"postcode")).toString());
63 address.setStreet(ao.value(QStringLiteral(
"road")).toString());
67static void injectExtra(QGeoLocation &location,
const QJsonObject &object)
70 static const QList<QString> extraKeys = { QStringLiteral(
"geojson"),
71 QStringLiteral(
"icon"),
72 QStringLiteral(
"importance"),
73 QStringLiteral(
"type"),
74 QStringLiteral(
"osm_id"),
75 QStringLiteral(
"osm_type"),
76 QStringLiteral(
"licence"),
77 QStringLiteral(
"place_id"),
78 QStringLiteral(
"class") };
80 for (
const auto &k: extraKeys) {
81 if (object.contains(k)) {
82 extra[k] = object.value(k).toVariant();
83 if (k == QStringLiteral(
"geojson"))
84 extra[QStringLiteral(
"geojson_model")] =
85 QGeoJson::importGeoJson(QJsonDocument::fromVariant(extra[k]));
89 location.setExtendedAttributes(extra);
92void QGeoCodeReplyOsm::networkReplyFinished()
94 QNetworkReply *reply =
static_cast<QNetworkReply *>(sender());
97 if (reply->error() != QNetworkReply::NoError)
100 QList<QGeoLocation> locations;
101 QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
103 if (document.isObject()) {
104 QJsonObject object = document.object();
106 QGeoCoordinate coordinate;
108 coordinate.setLatitude(object.value(QStringLiteral(
"lat")).toString().toDouble());
109 coordinate.setLongitude(object.value(QStringLiteral(
"lon")).toString().toDouble());
111 QGeoLocation location;
112 location.setCoordinate(coordinate);
113 location.setAddress(parseAddressObject(object));
115 if (m_includeExtraData)
116 injectExtra(location, object);
117 locations.append(location);
119 setLocations(locations);
120 }
else if (document.isArray()) {
121 QJsonArray results = document.array();
123 for (
int i = 0; i < results.count(); ++i) {
124 if (!results.at(i).isObject())
127 QJsonObject object = results.at(i).toObject();
129 QGeoCoordinate coordinate;
131 coordinate.setLatitude(object.value(QStringLiteral(
"lat")).toString().toDouble());
132 coordinate.setLongitude(object.value(QStringLiteral(
"lon")).toString().toDouble());
134 QGeoRectangle rectangle;
136 if (object.contains(QStringLiteral(
"boundingbox"))) {
137 QJsonArray a = object.value(QStringLiteral(
"boundingbox")).toArray();
138 if (a.count() == 4) {
139 rectangle.setTopLeft(QGeoCoordinate(a.at(1).toString().toDouble(),
140 a.at(2).toString().toDouble()));
141 rectangle.setBottomRight(QGeoCoordinate(a.at(0).toString().toDouble(),
142 a.at(3).toString().toDouble()));
146 QGeoLocation location;
147 location.setCoordinate(coordinate);
148 location.setBoundingShape(rectangle);
149 location.setAddress(parseAddressObject(object));
150 if (m_includeExtraData)
151 injectExtra(location, object);
152 locations.append(location);
157 setLocations(locations);
161void QGeoCodeReplyOsm::networkReplyError(QNetworkReply::NetworkError error)
164 QNetworkReply *reply =
static_cast<QNetworkReply *>(sender());
165 reply->deleteLater();
166 setError(QGeoCodeReply::CommunicationError, reply->errorString());
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
Combined button and popup list for selecting options.
static QGeoAddress parseAddressObject(const QJsonObject &object)
static void injectExtra(QGeoLocation &location, const QJsonObject &object)