1/****************************************************************************
2**
3** Copyright (C) 2017 Mapbox, Inc.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtLocation module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qgeocodingmanagerenginemapbox.h"
41#include "qgeocodereplymapbox.h"
42#include "qmapboxcommon.h"
43
44#include <QtCore/QVariantMap>
45#include <QtCore/QUrl>
46#include <QtCore/QUrlQuery>
47#include <QtCore/QLocale>
48#include <QtCore/QStringList>
49#include <QtNetwork/QNetworkAccessManager>
50#include <QtNetwork/QNetworkRequest>
51#include <QtNetwork/QNetworkReply>
52#include <QtPositioning/QGeoCoordinate>
53#include <QtPositioning/QGeoAddress>
54#include <QtPositioning/QGeoShape>
55#include <QtPositioning/QGeoCircle>
56#include <QtPositioning/QGeoRectangle>
57
58QT_BEGIN_NAMESPACE
59
60namespace {
61 static const QString allAddressTypes = QStringLiteral("address,district,locality,neighborhood,place,postcode,region,country");
62}
63
64QGeoCodingManagerEngineMapbox::QGeoCodingManagerEngineMapbox(const QVariantMap &parameters,
65 QGeoServiceProvider::Error *error,
66 QString *errorString)
67: QGeoCodingManagerEngine(parameters), m_networkManager(new QNetworkAccessManager(this))
68{
69 if (parameters.contains(QStringLiteral("mapbox.useragent")))
70 m_userAgent = parameters.value(QStringLiteral("mapbox.useragent")).toString().toLatin1();
71 else
72 m_userAgent = QByteArrayLiteral("Qt Location based application");
73
74 m_accessToken = parameters.value(QStringLiteral("mapbox.access_token")).toString();
75
76 m_isEnterprise = parameters.value(QStringLiteral("mapbox.enterprise")).toBool();
77 m_urlPrefix = m_isEnterprise ? mapboxGeocodingEnterpriseApiPath : mapboxGeocodingApiPath;
78
79 *error = QGeoServiceProvider::NoError;
80 errorString->clear();
81}
82
83QGeoCodingManagerEngineMapbox::~QGeoCodingManagerEngineMapbox()
84{
85}
86
87QGeoCodeReply *QGeoCodingManagerEngineMapbox::geocode(const QGeoAddress &address, const QGeoShape &bounds)
88{
89 QUrlQuery queryItems;
90
91 // If address text() is not generated: a manual setText() has been made.
92 if (!address.isTextGenerated()) {
93 queryItems.addQueryItem(QStringLiteral("type"), value: allAddressTypes);
94 return doSearch(address.text().simplified(), queryItems, bounds);
95 }
96
97 QStringList addressString;
98 QStringList typeString;
99
100 if (!address.street().isEmpty()) {
101 addressString.append(t: address.street());
102 typeString.append(QStringLiteral("address"));
103 }
104
105 if (!address.district().isEmpty()) {
106 addressString.append(t: address.district());
107 typeString.append(QStringLiteral("district"));
108 typeString.append(QStringLiteral("locality"));
109 typeString.append(QStringLiteral("neighborhood"));
110 }
111
112 if (!address.city().isEmpty()) {
113 addressString.append(t: address.city());
114 typeString.append(QStringLiteral("place"));
115 }
116
117 if (!address.postalCode().isEmpty()) {
118 addressString.append(t: address.postalCode());
119 typeString.append(QStringLiteral("postcode"));
120 }
121
122 if (!address.state().isEmpty()) {
123 addressString.append(t: address.state());
124 typeString.append(QStringLiteral("region"));
125 }
126
127 if (!address.country().isEmpty()) {
128 addressString.append(t: address.country());
129 typeString.append(QStringLiteral("country"));
130 }
131
132 queryItems.addQueryItem(QStringLiteral("type"), value: typeString.join(sep: QLatin1Char(',')));
133 queryItems.addQueryItem(QStringLiteral("limit"), value: QString::number(1));
134
135 return doSearch(addressString.join(QStringLiteral(", ")), queryItems, bounds);
136}
137
138QGeoCodeReply *QGeoCodingManagerEngineMapbox::geocode(const QString &address, int limit, int offset, const QGeoShape &bounds)
139{
140 Q_UNUSED(offset);
141
142 QUrlQuery queryItems;
143 queryItems.addQueryItem(QStringLiteral("type"), value: allAddressTypes);
144 queryItems.addQueryItem(QStringLiteral("limit"), value: QString::number(limit));
145
146 return doSearch(address, queryItems, bounds);
147}
148
149QGeoCodeReply *QGeoCodingManagerEngineMapbox::reverseGeocode(const QGeoCoordinate &coordinate, const QGeoShape &bounds)
150{
151 const QString coordinateString = QString::number(coordinate.longitude()) + QLatin1Char(',') + QString::number(coordinate.latitude());
152
153 QUrlQuery queryItems;
154 queryItems.addQueryItem(QStringLiteral("limit"), value: QString::number(1));
155
156 return doSearch(coordinateString, queryItems, bounds);
157}
158
159QGeoCodeReply *QGeoCodingManagerEngineMapbox::doSearch(const QString &request, QUrlQuery &queryItems, const QGeoShape &bounds)
160{
161 queryItems.addQueryItem(QStringLiteral("access_token"), value: m_accessToken);
162
163 const QString &languageCode = QLocale::system().name().section(asep: QLatin1Char('_'), astart: 0, aend: 0);
164 queryItems.addQueryItem(QStringLiteral("language"), value: languageCode);
165
166 QGeoRectangle boundingBox = bounds.boundingGeoRectangle();
167 if (!boundingBox.isEmpty()) {
168 queryItems.addQueryItem(QStringLiteral("bbox"),
169 value: QString::number(boundingBox.topLeft().longitude()) + QLatin1Char(',') +
170 QString::number(boundingBox.bottomRight().latitude()) + QLatin1Char(',') +
171 QString::number(boundingBox.bottomRight().longitude()) + QLatin1Char(',') +
172 QString::number(boundingBox.topLeft().latitude()));
173 }
174
175 QUrl requestUrl(m_urlPrefix + request + QStringLiteral(".json"));
176 requestUrl.setQuery(queryItems);
177
178 QNetworkRequest networkRequest(requestUrl);
179 networkRequest.setHeader(header: QNetworkRequest::UserAgentHeader, value: m_userAgent);
180
181 QNetworkReply *networkReply = m_networkManager->get(request: networkRequest);
182 QGeoCodeReplyMapbox *reply = new QGeoCodeReplyMapbox(networkReply, this);
183
184 connect(sender: reply, signal: &QGeoCodeReplyMapbox::finished, receiver: this, slot: &QGeoCodingManagerEngineMapbox::onReplyFinished);
185 connect(sender: reply, signal: QOverload<QGeoCodeReply::Error, const QString &>::of(ptr: &QGeoCodeReply::error),
186 receiver: this, slot: &QGeoCodingManagerEngineMapbox::onReplyError);
187
188 return reply;
189}
190
191void QGeoCodingManagerEngineMapbox::onReplyFinished()
192{
193 QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(object: sender());
194 if (reply)
195 emit finished(reply);
196}
197
198void QGeoCodingManagerEngineMapbox::onReplyError(QGeoCodeReply::Error errorCode, const QString &errorString)
199{
200 QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(object: sender());
201 if (reply)
202 emit error(reply, error: errorCode, errorString);
203}
204
205QT_END_NAMESPACE
206

source code of qtlocation/src/plugins/geoservices/mapbox/qgeocodingmanagerenginemapbox.cpp