8#include <QtCore/QVariantMap>
10#include <QtCore/QUrlQuery>
11#include <QtCore/QLocale>
12#include <QtCore/QStringList>
13#include <QtNetwork/QNetworkAccessManager>
14#include <QtNetwork/QNetworkRequest>
15#include <QtNetwork/QNetworkReply>
16#include <QtPositioning/QGeoCoordinate>
17#include <QtPositioning/QGeoAddress>
18#include <QtPositioning/QGeoShape>
19#include <QtPositioning/QGeoCircle>
20#include <QtPositioning/QGeoRectangle>
25 static const QString allAddressTypes = QStringLiteral(
"address,district,locality,neighborhood,place,postcode,region,country");
29 QGeoServiceProvider::Error *error,
31: QGeoCodingManagerEngine(parameters), m_networkManager(
new QNetworkAccessManager(
this))
33 if (parameters.contains(QStringLiteral(
"mapbox.useragent")))
34 m_userAgent = parameters.value(QStringLiteral(
"mapbox.useragent")).toString().toLatin1();
36 m_userAgent = QByteArrayLiteral(
"Qt Location based application");
38 m_accessToken = parameters.value(QStringLiteral(
"mapbox.access_token")).toString();
40 m_isEnterprise = parameters.value(QStringLiteral(
"mapbox.enterprise")).toBool();
41 m_urlPrefix = m_isEnterprise ? mapboxGeocodingEnterpriseApiPath : mapboxGeocodingApiPath;
43 *error = QGeoServiceProvider::NoError;
56 if (!address.isTextGenerated()) {
57 queryItems.addQueryItem(QStringLiteral(
"type"), allAddressTypes);
58 return doSearch(address.text().simplified(), queryItems, bounds);
61 QStringList addressString;
62 QStringList typeString;
64 if (!address.street().isEmpty()) {
65 addressString.append(address.street());
66 typeString.append(QStringLiteral(
"address"));
69 if (!address.district().isEmpty()) {
70 addressString.append(address.district());
71 typeString.append(QStringLiteral(
"district"));
72 typeString.append(QStringLiteral(
"locality"));
73 typeString.append(QStringLiteral(
"neighborhood"));
76 if (!address.city().isEmpty()) {
77 addressString.append(address.city());
78 typeString.append(QStringLiteral(
"place"));
81 if (!address.postalCode().isEmpty()) {
82 addressString.append(address.postalCode());
83 typeString.append(QStringLiteral(
"postcode"));
86 if (!address.state().isEmpty()) {
87 addressString.append(address.state());
88 typeString.append(QStringLiteral(
"region"));
91 if (!address.country().isEmpty()) {
92 addressString.append(address.country());
93 typeString.append(QStringLiteral(
"country"));
96 queryItems.addQueryItem(QStringLiteral(
"type"), typeString.join(QLatin1Char(
',')));
97 queryItems.addQueryItem(QStringLiteral(
"limit"), QString::number(1));
99 return doSearch(addressString.join(QStringLiteral(
", ")), queryItems, bounds);
106 QUrlQuery queryItems;
107 queryItems.addQueryItem(QStringLiteral(
"type"), allAddressTypes);
108 queryItems.addQueryItem(QStringLiteral(
"limit"), QString::number(limit));
110 return doSearch(address, queryItems, bounds);
115 const QString coordinateString = QString::number(coordinate.longitude()) + QLatin1Char(
',') + QString::number(coordinate.latitude());
117 QUrlQuery queryItems;
118 queryItems.addQueryItem(QStringLiteral(
"limit"), QString::number(1));
120 return doSearch(coordinateString, queryItems, bounds);
125 queryItems.addQueryItem(QStringLiteral(
"access_token"), m_accessToken);
127 const QString &languageCode = QLocale::system().name().section(QLatin1Char(
'_'), 0, 0);
128 queryItems.addQueryItem(QStringLiteral(
"language"), languageCode);
130 QGeoRectangle boundingBox = bounds.boundingGeoRectangle();
131 if (!boundingBox.isEmpty()) {
132 queryItems.addQueryItem(QStringLiteral(
"bbox"),
133 QString::number(boundingBox.topLeft().longitude()) + QLatin1Char(
',') +
134 QString::number(boundingBox.bottomRight().latitude()) + QLatin1Char(
',') +
135 QString::number(boundingBox.bottomRight().longitude()) + QLatin1Char(
',') +
136 QString::number(boundingBox.topLeft().latitude()));
139 QUrl requestUrl(m_urlPrefix + request + QStringLiteral(
".json"));
140 requestUrl.setQuery(queryItems);
142 QNetworkRequest networkRequest(requestUrl);
143 networkRequest.setHeader(QNetworkRequest::UserAgentHeader, m_userAgent);
145 QNetworkReply *networkReply = m_networkManager->get(networkRequest);
146 QGeoCodeReplyMapbox *reply =
new QGeoCodeReplyMapbox(networkReply,
this);
148 connect(reply, &QGeoCodeReplyMapbox::finished,
149 this, &QGeoCodingManagerEngineMapbox::onReplyFinished);
150 connect(reply, &QGeoCodeReply::errorOccurred,
151 this, &QGeoCodingManagerEngineMapbox::onReplyError);
158 QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
160 emit finished(reply);
165 QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
167 emit errorOccurred(reply, errorCode, errorString);
QGeoCodeReply * reverseGeocode(const QGeoCoordinate &coordinate, const QGeoShape &bounds) override
Begins the reverse geocoding of coordinate.
QGeoCodeReply * geocode(const QGeoAddress &address, const QGeoShape &bounds) override
Begins the geocoding of address.
~QGeoCodingManagerEngineMapbox()
QGeoCodeReply * geocode(const QString &address, int limit, int offset, const QGeoShape &bounds) override
Begins geocoding for a location matching address.
static const QString allAddressTypes