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
georoutingmanagerengine_esri.cpp
Go to the documentation of this file.
1// Copyright (C) 2013-2018 Esri <contracts@esri.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
7#include <QUrlQuery>
8
10
11static const QString kPrefixEsri(QStringLiteral("esri."));
14
16 "https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/solve"));
17
20 QString *errorString) :
21 QGeoRoutingManagerEngine(parameters), m_networkManager(new QNetworkAccessManager(this))
22{
23 if (parameters.contains(kParamUserAgent))
24 m_userAgent = parameters.value(kParamUserAgent).toString().toLatin1();
25 else
26 m_userAgent = QByteArrayLiteral("Qt Location based application");
27
28 m_token = parameters.value(kParamToken).toString();
29
31 errorString->clear();
32}
33
37
38// REST reference:
39// https://resources.arcgis.com/en/help/arcgis-rest-api/index.html#//02r300000036000000
40
42{
43 QNetworkRequest networkRequest;
44 networkRequest.setHeader(QNetworkRequest::UserAgentHeader, m_userAgent);
45
48 QString stops;
49
50 for (const QGeoCoordinate &coordinate : request.waypoints()) {
51 if (!stops.isEmpty())
52 stops += "; ";
53
54 stops += QString::number(coordinate.longitude()) + QLatin1Char(',') +
55 QString::number(coordinate.latitude());
56 }
57
58 query.addQueryItem(QStringLiteral("stops"), stops);
59 query.addQueryItem(QStringLiteral("f"), QStringLiteral("json"));
60 query.addQueryItem(QStringLiteral("directionsLanguage"), preferedDirectionLangage());
61 query.addQueryItem(QStringLiteral("directionsLengthUnits"), preferedDirectionsLengthUnits());
62 query.addQueryItem(QStringLiteral("token"), m_token);
63
65 networkRequest.setUrl(url);
66
67 QNetworkReply *reply = m_networkManager->get(networkRequest);
68 GeoRouteReplyEsri *routeReply = new GeoRouteReplyEsri(reply, request, this);
69
71 this, &GeoRoutingManagerEngineEsri::replyFinished);
73 this, &GeoRoutingManagerEngineEsri::replyError);
74
75 return routeReply;
76}
77
78void GeoRoutingManagerEngineEsri::replyFinished()
79{
80 QGeoRouteReply *reply = qobject_cast<QGeoRouteReply *>(sender());
81 if (reply)
83}
84
85void GeoRoutingManagerEngineEsri::replyError(QGeoRouteReply::Error errorCode, const QString &errorString)
86{
87 QGeoRouteReply *reply = qobject_cast<QGeoRouteReply *>(sender());
88 if (reply)
89 emit errorOccurred(reply, errorCode, errorString);
90}
91
92QString GeoRoutingManagerEngineEsri::preferedDirectionLangage()
93{
94 // list of supported langages is defined in:
95 // https://resources.arcgis.com/en/help/arcgis-rest-api/index.html#//02r300000036000000
96 const QStringList supportedLanguages = {
97 "ar", // Generate directions in Arabic
98 "cs", // Generate directions in Czech
99 "de", // Generate directions in German
100 "el", // Generate directions in Greek
101 "en", // Generate directions in English (default)
102 "es", // Generate directions in Spanish
103 "et", // Generate directions in Estonian
104 "fr", // Generate directions in French
105 "he", // Generate directions in Hebrew
106 "it", // Generate directions in Italian
107 "ja", // Generate directions in Japanese
108 "ko", // Generate directions in Korean
109 "lt", // Generate directions in Lithuanian
110 "lv", // Generate directions in Latvian
111 "nl", // Generate directions in Dutch
112 "pl", // Generate directions in Polish
113 "pt-BR", // Generate directions in Brazilian Portuguese
114 "pt-PT", // Generate directions in Portuguese (Portugal)
115 "ru", // Generate directions in Russian
116 "sv", // Generate directions in Swedish
117 "tr", // Generate directions in Turkish
118 "zh-CN" // Simplified Chinese
119 };
120
121 for (const QString &language: locale().uiLanguages())
122 {
123 if (language.startsWith("pt_BR")) // Portuguese (Brazilian)
124 return QStringLiteral("pt-BR");
125 if (language.startsWith("pt")) // Portuguese (Portugal)
126 return QStringLiteral("pt-PT");
127 if (language.startsWith("zh")) // Portuguese (Portugal)
128 return QStringLiteral("zh-CN");
129
130 const QString country = language.left(2);
131 if (supportedLanguages.contains(country))
132 return country;
133 }
134 return QStringLiteral("en"); // default value
135}
136
137QString GeoRoutingManagerEngineEsri::preferedDirectionsLengthUnits()
138{
139 switch (measurementSystem())
140 {
142 return QStringLiteral("esriNAUMeters");
143 break;
145 return QStringLiteral( "esriNAUFeet");
146 break;
148 return QStringLiteral("esriNAUFeet");
149 break;
150 default:
151 return QStringLiteral("esriNAUMeters");
152 break;
153 }
154 return QStringLiteral("esriNAUMeters");
155}
156
GeoRoutingManagerEngineEsri(const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString)
QGeoRouteReply * calculateRoute(const QGeoRouteRequest &request) override
\inmodule QtPositioning
\inmodule QtLocation
void finished()
This signal is emitted when this reply has finished processing.
Error
Describes an error which prevented the completion of the operation.
void errorOccurred(QGeoRouteReply::Error error, const QString &errorString=QString())
This signal is emitted when an error has been detected in the processing of this reply.
\inmodule QtLocation
QLocale::MeasurementSystem measurementSystem() const
Returns the measurement system used by this manager.
QLocale locale() const
Returns the locale used to hint to this routing manager about what language to use for addresses and ...
void errorOccurred(QGeoRouteReply *reply, QGeoRouteReply::Error error, const QString &errorString=QString())
This signal is emitted when an error has been detected in the processing of reply.
void finished(QGeoRouteReply *reply)
This signal is emitted when reply has finished processing.
Error
Describes an error related to the loading and setup of a service provider plugin.
@ ImperialUSSystem
Definition qlocale.h:869
@ MetricSystem
Definition qlocale.h:868
@ ImperialUKSystem
Definition qlocale.h:870
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.
void setHeader(KnownHeaders header, const QVariant &value)
Sets the value of the known header header to be value, overriding any previously set headers.
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
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
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
\inmodule QtCore
Definition qurl.h:94
void setQuery(const QString &query, ParsingMode mode=TolerantMode)
Sets the query string of the URL to query.
Definition qurl.cpp:2550
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
#define this
Definition dialogs.cpp:9
static const QString kParamUserAgent(kPrefixEsri+QStringLiteral("useragent"))
static const QString kParamUserAgent(kPrefixEsri+QStringLiteral("useragent"))
static const QString kParamToken(kPrefixEsri+QStringLiteral("token"))
static const QString kUrlRouting(QStringLiteral("https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/solve"))
static QT_BEGIN_NAMESPACE const QString kPrefixEsri(QStringLiteral("esri."))
Combined button and popup list for selecting options.
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
DBusConnection const char DBusError * error
GLenum query
#define QStringLiteral(str)
#define emit
QUrl url("example.com")
[constructor-url-reference]
QNetworkRequest request(url)
QNetworkReply * reply
\inmodule QtCore \reentrant
Definition qchar.h:18