4#include <QtCore/qjsondocument.h>
5#include <QtCore/qjsonobject.h>
6#include <QtCore/qlocale.h>
7#include <QtCore/qurlquery.h>
8#include <QtCore/qurl.h>
9#include <QtNetwork/qnetworkaccessmanager.h>
10#include <QtNetwork/qnetworkreply.h>
11#include <QtNetwork/qnetworkrequest.h>
12#include <QtPositioning/qgeoaddress.h>
13#include <QtPositioning/qgeocoordinate.h>
14#include <QtPositioning/qgeorectangle.h>
15#include <QtPositioning/qgeoshape.h>
16#include <qgeocodereplyohosmapkit.h>
17#include <qgeocodingmanagerengineohosmapkit.h>
18#include <qohosmapkitcommon.h>
32 const QVariantMap ¶meters, QGeoServiceProvider::Error *error, QString *errorString);
39 QGeoCodeReply *createGeoCodeReply(
const QUrl &requestUrl,
const QByteArray &requestBody);
41 void onReplyFinished();
42 void onReplyError(QGeoCodeReply::Error errorCode,
const QString &errorString);
44 QNetworkAccessManager m_networkManager;
46 QString m_authenticationKey;
50 const QVariantMap ¶meters, QGeoServiceProvider::Error *error, QString *errorString)
56 *error = QGeoServiceProvider::NoError;
58 if (errorString !=
nullptr)
63 const QGeoAddress &address,
const QGeoShape &bounds)
65 return geocode(address.text().simplified(), 0, 0, bounds);
69 const QString &address,
int limit,
int offset,
const QGeoShape &bounds)
74 if (address.isEmpty()) {
75 return new QGeoCodeReply(
76 QGeoCodeReply::CombinationError,
77 tr(
"Cannot do geocoding query using empty address"),
81 QJsonObject geocodeRequestBody;
83 constexpr int restApiMaxAddressLength = 512;
84 geocodeRequestBody.insert(QStringLiteral(
"address"), address.left(restApiMaxAddressLength));
86 auto coordinateBoundsObj =
87 OhosMapKit::CoordinateBoundsJson::tryConvertFromQGeoRectangle(bounds.boundingGeoRectangle());
88 if (!coordinateBoundsObj.isEmpty())
89 geocodeRequestBody.insert(QStringLiteral(
"bounds"), coordinateBoundsObj);
91 geocodeRequestBody.insert(QStringLiteral(
"language"), OhosMapKit::getLanguageCode());
93 return createGeoCodeReply(
94 QUrl(geocodeApiPath), QJsonDocument(geocodeRequestBody).toJson(QJsonDocument::Compact));
98 const QGeoCoordinate &coordinate,
const QGeoShape &bounds)
100 if (!coordinate.isValid()) {
101 return new QGeoCodeReply(
102 QGeoCodeReply::CombinationError,
103 tr(
"Cannot do reverse geocoding query using non-valid coordinates"),
107 QJsonObject reverseGeocodeRequestBody;
108 reverseGeocodeRequestBody.insert(
109 QStringLiteral(
"location"), OhosMapKit::CoordinateJson::tryConvertFromQGeoCoordinate(coordinate));
110 reverseGeocodeRequestBody.insert(QStringLiteral(
"language"), OhosMapKit::getLanguageCode());
112 if (bounds.isValid() && !bounds.isEmpty()) {
113 constexpr int restApiMinRadiusInMeters = 0;
114 constexpr int restApiMaxRadiusInMeters = 50;
115 const int radius = bounds.center().distanceTo(bounds.boundingGeoRectangle().topLeft());
116 reverseGeocodeRequestBody.insert(
117 QStringLiteral(
"radius"),
118 qBound(restApiMinRadiusInMeters, radius, restApiMaxRadiusInMeters));
121 return createGeoCodeReply(
122 QUrl(reverseGeocodeApiPath),
123 QJsonDocument(reverseGeocodeRequestBody).toJson(QJsonDocument::Compact));
127 const QUrl &requestUrl,
const QByteArray &requestBody)
129 auto *reply = makeQGeoCodeReplyOhosMapKit(
130 m_networkManager.post(
131 OhosMapKit::createOhosMapKitNetworkRequestWithJsonBody(
132 requestUrl, m_userAgent, m_authenticationKey),
137 reply, &QGeoCodeReply::finished,
140 reply, &QGeoCodeReply::errorOccurred,
141 this, &QGeoCodingManagerEngineOhosMapKit::onReplyError);
148 auto *geocodeReply = qobject_cast<QGeoCodeReply *>(sender());
149 if (geocodeReply !=
nullptr)
150 Q_EMIT finished(geocodeReply);
154 QGeoCodeReply::Error errorCode,
const QString &errorString)
156 auto *geocodeReply = qobject_cast<QGeoCodeReply *>(sender());
157 if (geocodeReply !=
nullptr)
158 Q_EMIT errorOccurred(geocodeReply, errorCode, errorString);
164 const QVariantMap ¶meters, QGeoServiceProvider::Error *error, QString *errorString)
QGeoCodingManagerEngineOhosMapKit(const QVariantMap ¶meters, QGeoServiceProvider::Error *error, QString *errorString)
QGeoCodeReply * geocode(const QGeoAddress &address, const QGeoShape &bounds) override
Begins the geocoding of address.
QGeoCodeReply * geocode(const QString &address, int limit, int offset, const QGeoShape &bounds) override
Begins geocoding for a location matching address.
QGeoCodeReply * reverseGeocode(const QGeoCoordinate &coordinate, const QGeoShape &bounds) override
Begins the reverse geocoding of coordinate.
Combined button and popup list for selecting options.
const QString reverseGeocodeApiPath
const QString geocodeApiPath
const QString geocodeApiBasePath
QGeoCodingManagerEngine * makeQGeoCodingManagerEngineOhosMapKit(const QVariantMap ¶meters, QGeoServiceProvider::Error *error, QString *errorString)