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
qgeocodingmanagerenginemapbox.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 Mapbox, Inc.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
7
8#include <QtCore/QVariantMap>
9#include <QtCore/QUrl>
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>
21
23
24namespace {
25 static const QString allAddressTypes = QStringLiteral("address,district,locality,neighborhood,place,postcode,region,country");
26}
27
28QGeoCodingManagerEngineMapbox::QGeoCodingManagerEngineMapbox(const QVariantMap &parameters,
29 QGeoServiceProvider::Error *error,
30 QString *errorString)
31: QGeoCodingManagerEngine(parameters), m_networkManager(new QNetworkAccessManager(this))
32{
33 if (parameters.contains(QStringLiteral("mapbox.useragent")))
34 m_userAgent = parameters.value(QStringLiteral("mapbox.useragent")).toString().toLatin1();
35 else
36 m_userAgent = QByteArrayLiteral("Qt Location based application");
37
38 m_accessToken = parameters.value(QStringLiteral("mapbox.access_token")).toString();
39
40 m_isEnterprise = parameters.value(QStringLiteral("mapbox.enterprise")).toBool();
41 m_urlPrefix = m_isEnterprise ? mapboxGeocodingEnterpriseApiPath : mapboxGeocodingApiPath;
42
43 *error = QGeoServiceProvider::NoError;
44 errorString->clear();
45}
46
50
51QGeoCodeReply *QGeoCodingManagerEngineMapbox::geocode(const QGeoAddress &address, const QGeoShape &bounds)
52{
53 QUrlQuery queryItems;
54
55 // If address text() is not generated: a manual setText() has been made.
56 if (!address.isTextGenerated()) {
57 queryItems.addQueryItem(QStringLiteral("type"), allAddressTypes);
58 return doSearch(address.text().simplified(), queryItems, bounds);
59 }
60
61 QStringList addressString;
62 QStringList typeString;
63
64 if (!address.street().isEmpty()) {
65 addressString.append(address.street());
66 typeString.append(QStringLiteral("address"));
67 }
68
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"));
74 }
75
76 if (!address.city().isEmpty()) {
77 addressString.append(address.city());
78 typeString.append(QStringLiteral("place"));
79 }
80
81 if (!address.postalCode().isEmpty()) {
82 addressString.append(address.postalCode());
83 typeString.append(QStringLiteral("postcode"));
84 }
85
86 if (!address.state().isEmpty()) {
87 addressString.append(address.state());
88 typeString.append(QStringLiteral("region"));
89 }
90
91 if (!address.country().isEmpty()) {
92 addressString.append(address.country());
93 typeString.append(QStringLiteral("country"));
94 }
95
96 queryItems.addQueryItem(QStringLiteral("type"), typeString.join(QLatin1Char(',')));
97 queryItems.addQueryItem(QStringLiteral("limit"), QString::number(1));
98
99 return doSearch(addressString.join(QStringLiteral(", ")), queryItems, bounds);
100}
101
102QGeoCodeReply *QGeoCodingManagerEngineMapbox::geocode(const QString &address, int limit, int offset, const QGeoShape &bounds)
103{
104 Q_UNUSED(offset);
105
106 QUrlQuery queryItems;
107 queryItems.addQueryItem(QStringLiteral("type"), allAddressTypes);
108 queryItems.addQueryItem(QStringLiteral("limit"), QString::number(limit));
109
110 return doSearch(address, queryItems, bounds);
111}
112
113QGeoCodeReply *QGeoCodingManagerEngineMapbox::reverseGeocode(const QGeoCoordinate &coordinate, const QGeoShape &bounds)
114{
115 const QString coordinateString = QString::number(coordinate.longitude()) + QLatin1Char(',') + QString::number(coordinate.latitude());
116
117 QUrlQuery queryItems;
118 queryItems.addQueryItem(QStringLiteral("limit"), QString::number(1));
119
120 return doSearch(coordinateString, queryItems, bounds);
121}
122
123QGeoCodeReply *QGeoCodingManagerEngineMapbox::doSearch(const QString &request, QUrlQuery &queryItems, const QGeoShape &bounds)
124{
125 queryItems.addQueryItem(QStringLiteral("access_token"), m_accessToken);
126
127 const QString &languageCode = QLocale::system().name().section(QLatin1Char('_'), 0, 0);
128 queryItems.addQueryItem(QStringLiteral("language"), languageCode);
129
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()));
137 }
138
139 QUrl requestUrl(m_urlPrefix + request + QStringLiteral(".json"));
140 requestUrl.setQuery(queryItems);
141
142 QNetworkRequest networkRequest(requestUrl);
143 networkRequest.setHeader(QNetworkRequest::UserAgentHeader, m_userAgent);
144
145 QNetworkReply *networkReply = m_networkManager->get(networkRequest);
146 QGeoCodeReplyMapbox *reply = new QGeoCodeReplyMapbox(networkReply, this);
147
148 connect(reply, &QGeoCodeReplyMapbox::finished,
149 this, &QGeoCodingManagerEngineMapbox::onReplyFinished);
150 connect(reply, &QGeoCodeReply::errorOccurred,
151 this, &QGeoCodingManagerEngineMapbox::onReplyError);
152
153 return reply;
154}
155
156void QGeoCodingManagerEngineMapbox::onReplyFinished()
157{
158 QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
159 if (reply)
160 emit finished(reply);
161}
162
163void QGeoCodingManagerEngineMapbox::onReplyError(QGeoCodeReply::Error errorCode, const QString &errorString)
164{
165 QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
166 if (reply)
167 emit errorOccurred(reply, errorCode, errorString);
168}
169
170QT_END_NAMESPACE
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.
QGeoCodeReply * geocode(const QString &address, int limit, int offset, const QGeoShape &bounds) override
Begins geocoding for a location matching address.