Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qplacemanagerengineohosmapkit.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
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>
16
17QT_BEGIN_NAMESPACE
18
19namespace {
20
22 QLatin1String("https://siteapi.cloud.huawei.com/mapApi/v1/siteService/");
23const auto searchByTextApiUrl = QUrl(placeSearchApiUrlBase + QLatin1String("searchByText"));
24const auto autoCompleteApiUrl = QUrl(placeSearchApiUrlBase + QLatin1String("queryAutoComplete"));
25
27{
28public:
30 const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString);
31
32 QPlaceSearchReply *search(const QPlaceSearchRequest &) override;
33 QPlaceSearchSuggestionReply *searchSuggestions(const QPlaceSearchRequest &request) override;
34
36 QString parentCategoryId(const QString &categoryId) const override;
37 QStringList childCategoryIds(const QString &categoryId) const override;
38 QPlaceCategory category(const QString &categoryId) const override;
39 QList<QPlaceCategory> childCategories(const QString &parentId) const override;
40
41private:
42 void onReplyFinished();
43 void onReplyError(QPlaceReply::Error errorCode, const QString &errorString);
44
45 QNetworkAccessManager m_networkManager;
46 QString m_userAgent;
47 QString m_authenticationKey;
48};
49
51{
52public:
53 DummyPlaceReply(QObject *parent);
54
55 void finish();
56};
57
61
63{
64 setFinished(true);
65 Q_EMIT finished();
66}
67
68QJsonObject createPlaceSearchRequestBody(const QPlaceSearchRequest &request)
69{
70 QJsonObject placeSearchRequestBody;
71
72 placeSearchRequestBody.insert(QStringLiteral("query"), request.searchTerm());
73
74 auto searchArea = request.searchArea();
75 if (searchArea.isValid() && !searchArea.isEmpty()) {
76 constexpr int restApiMinRadiusInMeters = 1;
77 constexpr int restApiMaxRadiusInMeters = 50000;
78 auto radius = qBound(
79 restApiMinRadiusInMeters,
80 qRound(searchArea.center().distanceTo(searchArea.boundingGeoRectangle().topLeft())),
81 restApiMaxRadiusInMeters);
82
83 placeSearchRequestBody.insert(
84 QStringLiteral("location"),
85 OhosMapKit::CoordinateJson::tryConvertFromQGeoCoordinate(searchArea.center()));
86 placeSearchRequestBody.insert(QStringLiteral("radius"), radius);
87 }
88
89 placeSearchRequestBody.insert(QStringLiteral("language"), OhosMapKit::getLanguageCode());
90
91 return placeSearchRequestBody;
92}
93
94QString convertCategoryIdToCategoryName(const QString &categoryId)
95{
96 auto base = categoryId;
97 if (!base.startsWith("ADMINISTRATIVE_AREA"))
98 base.remove(QRegularExpression(R"((_LEVEL_\d+)$)"));
99
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();
105 }
106 return parts.join(QLatin1Char(' '));
107}
108
109
111 const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString)
115{
116 if (error != nullptr)
117 *error = QGeoServiceProvider::NoError;
118
119 if (errorString != nullptr)
120 errorString->clear();
121}
122
123QPlaceSearchReply *QPlaceManagerEngineOhosMapKit::search(const QPlaceSearchRequest &request)
124{
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."),
129 this);
130 }
131
132 auto placeSearchRequestBody = createPlaceSearchRequestBody(request);
133
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));
140 }
141
142 if (!request.categories().isEmpty())
143 placeSearchRequestBody.insert(QStringLiteral("hwPoiType"), request.categories().first().categoryId());
144
145 auto *reply = makeQPlaceSearchReplyOhosMapKit(
146 request,
147 m_networkManager.post(
148 OhosMapKit::createOhosMapKitNetworkRequestWithJsonBody(
149 searchByTextApiPath, m_userAgent, m_authenticationKey),
150 QJsonDocument(placeSearchRequestBody).toJson(QJsonDocument::Compact)),
151 this);
152
153 connect(
154 reply, &QPlaceSearchReply::finished,
155 this, &QPlaceManagerEngineOhosMapKit::onReplyFinished);
156 connect(
157 reply,
158 &QPlaceSearchReply::errorOccurred,
159 this,
160 &QPlaceManagerEngineOhosMapKit::onReplyError);
161
162 return reply;
163}
164
166 const QPlaceSearchRequest &request)
167{
168 auto *reply = makeQPlaceSearchSuggestionReplyOhosMapKit(
169 m_networkManager.post(
170 OhosMapKit::createOhosMapKitNetworkRequestWithJsonBody(
171 autoCompleteApiPath, m_userAgent, m_authenticationKey),
172 QJsonDocument(createPlaceSearchRequestBody(request)).toJson(QJsonDocument::Compact)),
173 this);
174
175 connect(
176 reply, &QPlaceSearchSuggestionReply::finished,
177 this, &QPlaceManagerEngineOhosMapKit::onReplyFinished);
178 connect(
179 reply,
180 &QPlaceSearchSuggestionReply::errorOccurred,
181 this,
182 &QPlaceManagerEngineOhosMapKit::onReplyError);
183
184 return reply;
185}
186
188{
189 auto *reply = new DummyPlaceReply(this);
190 connect(reply, &QPlaceReply::finished, this, &QPlaceManagerEngineOhosMapKit::onReplyFinished);
191 QMetaObject::invokeMethod(reply, &DummyPlaceReply::finish, Qt::QueuedConnection);
192
193 return reply;
194}
195
197{
198 return QPoiCategoryOhosMapKit::tryGetParentCategoryIdOrEmpty(categoryId);
199}
200
202{
203 return QPoiCategoryOhosMapKit::tryGetChildCategoryIdsOrEmpty(categoryId);
204}
205
207{
208 QPlaceCategory placeCategory;
209 placeCategory.setCategoryId(categoryId);
210 placeCategory.setName(convertCategoryIdToCategoryName(categoryId));
211 return placeCategory;
212}
213
215{
216 auto categoryIds = childCategoryIds(parentId);
217
218 QList<QPlaceCategory> childCategories;
219 for (const auto &categoryId : categoryIds)
220 childCategories.push_back(category(categoryId));
221
222 return childCategories;
223}
224
225void QPlaceManagerEngineOhosMapKit::onReplyFinished()
226{
227 auto *reply = qobject_cast<QPlaceReply *>(sender());
228 if (reply != nullptr)
229 Q_EMIT finished(reply);
230}
231
232void QPlaceManagerEngineOhosMapKit::onReplyError(
233 QPlaceReply::Error errorCode, const QString &errorString)
234{
235 auto *reply = qobject_cast<QPlaceReply *>(sender());
236 if (reply != nullptr)
237 Q_EMIT errorOccurred(reply, errorCode, errorString);
238}
239
240}
241
243 const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString)
244{
245 return new QPlaceManagerEngineOhosMapKit(parameters, error, errorString);
246}
247
248QT_END_NAMESPACE
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 &parameters, 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.
QJsonObject createPlaceSearchRequestBody(const QPlaceSearchRequest &request)
QString convertCategoryIdToCategoryName(const QString &categoryId)
QPlaceManagerEngine * makeQPlaceManagerEngineOhosMapKit(const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString)