4#include <QtCore/qjsondocument.h>
5#include <QtCore/qjsonobject.h>
6#include <QtCore/qregularexpression.h>
7#include <QtCore/qstringlist.h>
8#include <QtLocation/private/unsupportedreplies_p.h>
9#include <QtLocation/qplacecategory.h>
10#include <QtNetwork/qnetworkaccessmanager.h>
11#include <qplacemanagerengineohosmapkit.h>
12#include <qplacesearchreplyohosmapkit.h>
13#include <qplacesearchsuggestionreplyohosmapkit.h>
14#include <qohosmapkitcommon.h>
15#include <qpoicategoryohosmapkit.h>
22 QLatin1String(
"https://siteapi.cloud.huawei.com/mapApi/v1/siteService/");
30 const QVariantMap ¶meters, QGeoServiceProvider::Error *error, QString *errorString);
42 void onReplyFinished();
43 void onReplyError(QPlaceReply::Error errorCode,
const QString &errorString);
45 QNetworkAccessManager m_networkManager;
47 QString m_authenticationKey;
70 QJsonObject placeSearchRequestBody;
72 placeSearchRequestBody.insert(QStringLiteral(
"query"), request.searchTerm());
74 auto searchArea = request.searchArea();
75 if (searchArea.isValid() && !searchArea.isEmpty()) {
76 constexpr int restApiMinRadiusInMeters = 1;
77 constexpr int restApiMaxRadiusInMeters = 50000;
79 restApiMinRadiusInMeters,
80 qRound(searchArea.center().distanceTo(searchArea.boundingGeoRectangle().topLeft())),
81 restApiMaxRadiusInMeters);
83 placeSearchRequestBody.insert(
84 QStringLiteral(
"location"),
85 OhosMapKit::CoordinateJson::tryConvertFromQGeoCoordinate(searchArea.center()));
86 placeSearchRequestBody.insert(QStringLiteral(
"radius"), radius);
89 placeSearchRequestBody.insert(QStringLiteral(
"language"), OhosMapKit::getLanguageCode());
91 return placeSearchRequestBody;
96 auto base = categoryId;
97 if (!base.startsWith(
"ADMINISTRATIVE_AREA"))
98 base.remove(QRegularExpression(R"((_LEVEL_\d+)$)"));
100 auto parts = base.split(QLatin1Char(
'_'), Qt::SkipEmptyParts);
101 for (
int i = 0; i < parts.size(); ++i) {
102 parts[i] = parts[i].toLower();
103 if (i == 0 && !parts[i].isEmpty())
104 parts[i][0] = parts[i][0].toUpper();
106 return parts.join(QLatin1Char(
' '));
111 const QVariantMap ¶meters, QGeoServiceProvider::Error *error, QString *errorString)
116 if (error !=
nullptr)
117 *error = QGeoServiceProvider::NoError;
119 if (errorString !=
nullptr)
120 errorString->clear();
125 if (request.categories().size() > 1) {
126 return new QPlaceSearchReplyUnsupported(
127 QPlaceReply::UnsupportedError,
128 tr(
"OHOS Map Kit allows search of only one category type."),
132 auto placeSearchRequestBody = createPlaceSearchRequestBody(request);
134 constexpr auto restApiMinNumOfResultsPerPage = 1;
135 constexpr auto restApiMaxNumOfResultsPerPage = 20;
136 if (request.limit() > 0) {
137 placeSearchRequestBody.insert(
138 QStringLiteral(
"pageSize"),
139 qBound(restApiMinNumOfResultsPerPage, request.limit(), restApiMaxNumOfResultsPerPage));
142 if (!request.categories().isEmpty())
143 placeSearchRequestBody.insert(QStringLiteral(
"hwPoiType"), request.categories().first().categoryId());
145 auto *reply = makeQPlaceSearchReplyOhosMapKit(
147 m_networkManager.post(
148 OhosMapKit::createOhosMapKitNetworkRequestWithJsonBody(
149 searchByTextApiPath, m_userAgent, m_authenticationKey),
150 QJsonDocument(placeSearchRequestBody).toJson(QJsonDocument::Compact)),
154 reply, &QPlaceSearchReply::finished,
158 &QPlaceSearchReply::errorOccurred,
160 &QPlaceManagerEngineOhosMapKit::onReplyError);
166 const QPlaceSearchRequest &request)
168 auto *reply = makeQPlaceSearchSuggestionReplyOhosMapKit(
169 m_networkManager.post(
170 OhosMapKit::createOhosMapKitNetworkRequestWithJsonBody(
171 autoCompleteApiPath, m_userAgent, m_authenticationKey),
172 QJsonDocument(createPlaceSearchRequestBody(request)).toJson(QJsonDocument::Compact)),
176 reply, &QPlaceSearchSuggestionReply::finished,
180 &QPlaceSearchSuggestionReply::errorOccurred,
182 &QPlaceManagerEngineOhosMapKit::onReplyError);
189 auto *reply =
new DummyPlaceReply(
this);
191 QMetaObject::invokeMethod(reply, &DummyPlaceReply::finish, Qt::QueuedConnection);
198 return QPoiCategoryOhosMapKit::tryGetParentCategoryIdOrEmpty(categoryId);
203 return QPoiCategoryOhosMapKit::tryGetChildCategoryIdsOrEmpty(categoryId);
208 QPlaceCategory placeCategory;
209 placeCategory.setCategoryId(categoryId);
210 placeCategory.setName(convertCategoryIdToCategoryName(categoryId));
211 return placeCategory;
216 auto categoryIds = childCategoryIds(parentId);
218 QList<QPlaceCategory> childCategories;
219 for (
const auto &categoryId : categoryIds)
220 childCategories.push_back(category(categoryId));
222 return childCategories;
227 auto *reply = qobject_cast<QPlaceReply *>(sender());
228 if (reply !=
nullptr)
229 Q_EMIT finished(reply);
233 QPlaceReply::Error errorCode,
const QString &errorString)
235 auto *reply = qobject_cast<QPlaceReply *>(sender());
236 if (reply !=
nullptr)
237 Q_EMIT errorOccurred(reply, errorCode, errorString);
243 const QVariantMap ¶meters, QGeoServiceProvider::Error *error, QString *errorString)
DummyPlaceReply(QObject *parent)
QStringList childCategoryIds(const QString &categoryId) const override
Returns the child category identifiers of the category corresponding to categoryId.
QPlaceReply * initializeCategories() override
Initializes the categories of the manager engine.
QPlaceCategory category(const QString &categoryId) const override
Returns the category corresponding to the given categoryId.
QPlaceSearchReply * search(const QPlaceSearchRequest &) override
Searches for places according to the parameters specified in request.
QPlaceSearchSuggestionReply * searchSuggestions(const QPlaceSearchRequest &request) override
Requests a set of search term suggestions according to the parameters specified in request.
QPlaceManagerEngineOhosMapKit(const QVariantMap ¶meters, QGeoServiceProvider::Error *error, QString *errorString)
QList< QPlaceCategory > childCategories(const QString &parentId) const override
Returns a list of categories that are children of the category corresponding to parentId.
QString parentCategoryId(const QString &categoryId) const override
Returns the parent category identifier of the category corresponding to categoryId.
const auto searchByTextApiUrl
const QString placeSearchApiUrlBase
QJsonObject createPlaceSearchRequestBody(const QPlaceSearchRequest &request)
QString convertCategoryIdToCategoryName(const QString &categoryId)
const auto autoCompleteApiUrl
QPlaceManagerEngine * makeQPlaceManagerEngineOhosMapKit(const QVariantMap ¶meters, QGeoServiceProvider::Error *error, QString *errorString)