Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
6#include "qmapboxcommon.h"
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
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
44 errorString->clear();
45}
46
50
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
103{
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
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
149 this, &QGeoCodingManagerEngineMapbox::onReplyFinished);
151 this, &QGeoCodingManagerEngineMapbox::onReplyError);
152
153 return reply;
154}
155
156void QGeoCodingManagerEngineMapbox::onReplyFinished()
157{
158 QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
159 if (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
\inmodule QtPositioning
Definition qgeoaddress.h:18
\inmodule QtLocation
void finished()
This signal is emitted when this reply has finished processing.
void errorOccurred(QGeoCodeReply::Error error, const QString &errorString=QString())
This signal is emitted when an error has been detected in the processing of this reply.
Error
Describes an error which prevented the completion of the operation.
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(const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString)
void errorOccurred(QGeoCodeReply *reply, QGeoCodeReply::Error error, const QString &errorString=QString())
void finished(QGeoCodeReply *reply)
\inmodule QtPositioning
double longitude
This property holds the longitude in decimal degrees.
double latitude
This property holds the latitude in decimal degrees.
\inmodule QtPositioning
QGeoCoordinate topLeft
This property holds the top left coordinate of this geo rectangle.
QGeoCoordinate bottomRight
This property holds the bottom right coordinate of this geo rectangle.
Error
Describes an error related to the loading and setup of a service provider plugin.
\inmodule QtPositioning
Definition qgeoshape.h:17
Q_INVOKABLE QGeoRectangle boundingGeoRectangle() const
Returns a QGeoRectangle representing the geographical bounding rectangle of the geo shape,...
bool isEmpty
This property defines whether this geo shape is empty.
Definition qgeoshape.h:21
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition qlocale.cpp:2862
T value(const Key &key, const T &defaultValue=T()) const
Definition qmap.h:357
bool contains(const Key &key) const
Definition qmap.h:341
The QNetworkAccessManager class allows the application to send network requests and receive replies.
QNetworkReply * get(const QNetworkRequest &request)
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object o...
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition qobject.cpp:2658
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QByteArray toLatin1() const &
Definition qstring.h:630
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
\inmodule QtCore
Definition qurlquery.h:20
void addQueryItem(const QString &key, const QString &value)
Appends the pair key = value to the end of the query string of the URL.
\inmodule QtCore
Definition qurl.h:94
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
bool toBool() const
Returns the variant as a bool if the variant has userType() Bool.
#define this
Definition dialogs.cpp:9
Combined button and popup list for selecting options.
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
DBusConnection const char DBusError * error
static const QString mapboxGeocodingApiPath
static const QString mapboxGeocodingEnterpriseApiPath
GLenum GLuint GLintptr offset
GLint limit
GLuint GLuint64EXT address
#define QStringLiteral(str)
#define emit
#define Q_UNUSED(x)
QNetworkRequest request(url)
QNetworkReply * reply
\inmodule QtCore \reentrant
Definition qchar.h:18