summaryrefslogtreecommitdiff
path: root/src/location/maps
diff options
context:
space:
mode:
authorBruno de Oliveira Abinader <bruno@mapbox.com>2018-04-09 16:37:38 +0300
committerPaolo Angelelli <paolo.angelelli@qt.io>2018-04-11 09:26:40 +0000
commitfdce040790a070701b89aed8c1c798ba183215dd (patch)
tree2f83240370617a08e8745b9cef3669e15a7fc541 /src/location/maps
parenta4d9f8cbf2220fbc8aafd8f491313e39e0bf4a5f (diff)
downloadqtlocation-fdce040790a070701b89aed8c1c798ba183215dd.tar.gz
Support Mapbox Directions API voice & banner instructions
Change-Id: I89f4f9940c2a9a862ffaec066e5a7841bc00bd05 Reviewed-by: Bruno de Oliveira Abinader <brunoabinader@gmail.com> Reviewed-by: Paolo Angelelli <paolo.angelelli@qt.io>
Diffstat (limited to 'src/location/maps')
-rw-r--r--src/location/maps/qgeorouteparserosrmv5.cpp110
-rw-r--r--src/location/maps/qgeorouteparserosrmv5_p.h20
2 files changed, 78 insertions, 52 deletions
diff --git a/src/location/maps/qgeorouteparserosrmv5.cpp b/src/location/maps/qgeorouteparserosrmv5.cpp
index 7cd29f92..58299d09 100644
--- a/src/location/maps/qgeorouteparserosrmv5.cpp
+++ b/src/location/maps/qgeorouteparserosrmv5.cpp
@@ -114,7 +114,7 @@ static QString exitOrdinal(int exit)
static QList<QString> ordinals;
if (!ordinals.size()) {
- ordinals.append(QStringLiteral(""));
+ ordinals.append(QLatin1String(""));
//: always used in " and take the %1 exit [onto <street name>]"
ordinals.append(QGeoRouteParserOsrmV5::tr("first", "roundabout exit"));
ordinals.append(QGeoRouteParserOsrmV5::tr("second", "roundabout exit"));
@@ -720,7 +720,7 @@ static QString instructionText(const QJsonObject &step, const QJsonObject &maneu
QString maneuverType;
if (maneuver.value(QLatin1String("type")).isString())
maneuverType = maneuver.value(QLatin1String("type")).toString();
- QString wayName = QStringLiteral("unknown street");
+ QString wayName = QLatin1String("unknown street");
if (step.value(QLatin1String("name")).isString())
wayName = step.value(QLatin1String("name")).toString();
@@ -793,7 +793,35 @@ static QGeoManeuver::InstructionDirection instructionDirection(const QJsonObject
return QGeoManeuver::NoDirection;
}
-static QGeoRouteSegment parseStep(const QJsonObject &step, bool useServerText, int legIndex, int stepIndex) {
+class QGeoRouteParserOsrmV5Private : public QGeoRouteParserPrivate
+{
+ Q_DECLARE_PUBLIC(QGeoRouteParserOsrmV5)
+public:
+ QGeoRouteParserOsrmV5Private();
+ virtual ~QGeoRouteParserOsrmV5Private();
+
+ QGeoRouteSegment parseStep(const QJsonObject &step, int legIndex, int stepIndex) const;
+
+ // QGeoRouteParserPrivate
+
+ QGeoRouteReply::Error parseReply(QList<QGeoRoute> &routes, QString &errorString, const QByteArray &reply) const override;
+ QUrl requestUrl(const QGeoRouteRequest &request, const QString &prefix) const override;
+
+ QVariantMap m_vendorParams;
+ const QGeoRouteParserOsrmV5Extension *m_extension = nullptr;
+};
+
+QGeoRouteParserOsrmV5Private::QGeoRouteParserOsrmV5Private()
+ : QGeoRouteParserPrivate()
+{
+}
+
+QGeoRouteParserOsrmV5Private::~QGeoRouteParserOsrmV5Private()
+{
+ delete m_extension;
+}
+
+QGeoRouteSegment QGeoRouteParserOsrmV5Private::parseStep(const QJsonObject &step, int legIndex, int stepIndex) const {
// OSRM Instructions documentation: https://github.com/Project-OSRM/osrm-text-instructions
// This goes on top of OSRM: https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md
// Mapbox however, includes this in the reply, under "instruction".
@@ -810,10 +838,6 @@ static QGeoRouteSegment parseStep(const QJsonObject &step, bool useServerText, i
if (!maneuver.value(QLatin1String("location")).isArray())
return segment;
- QString instruction_text;
- if (maneuver.value(QLatin1String("instruction")).isString())
- instruction_text = maneuver.value(QLatin1String("instruction")).toString();
-
double time = step.value(QLatin1String("duration")).toDouble();
double distance = step.value(QLatin1String("distance")).toDouble();
@@ -827,11 +851,15 @@ static QGeoRouteSegment parseStep(const QJsonObject &step, bool useServerText, i
QString geometry = step.value(QLatin1String("geometry")).toString();
QList<QGeoCoordinate> path = decodePolyline(geometry);
+ QGeoManeuver::InstructionDirection maneuverInstructionDirection = instructionDirection(maneuver);
+
+ QString maneuverInstructionText = instructionText(step, maneuver, maneuverInstructionDirection);
+
QGeoManeuver geoManeuver;
- geoManeuver.setDirection(instructionDirection(maneuver));
+ geoManeuver.setDirection(maneuverInstructionDirection);
geoManeuver.setDistanceToNextInstruction(distance);
geoManeuver.setTimeToNextInstruction(time);
- geoManeuver.setInstructionText((useServerText && !instruction_text.isEmpty()) ? instruction_text : instructionText(step, maneuver, geoManeuver.direction()));
+ geoManeuver.setInstructionText(maneuverInstructionText);
geoManeuver.setPosition(coord);
geoManeuver.setWaypoint(coord);
@@ -850,45 +878,27 @@ static QGeoRouteSegment parseStep(const QJsonObject &step, bool useServerText, i
// Ref: http://project-osrm.org/docs/v5.15.2/api/#routeleg-object
extraAttributes.insert(QLatin1String("leg_index"), legIndex);
extraAttributes.insert(QLatin1String("step_index"), stepIndex);
+
geoManeuver.setExtendedAttributes(extraAttributes);
segment.setDistance(distance);
segment.setPath(path);
segment.setTravelTime(time);
segment.setManeuver(geoManeuver);
+ if (m_extension)
+ m_extension->updateSegment(segment, step, maneuver);
return segment;
}
-class QGeoRouteParserOsrmV5Private : public QGeoRouteParserPrivate
-{
- Q_DECLARE_PUBLIC(QGeoRouteParserOsrmV5)
-public:
- QGeoRouteParserOsrmV5Private();
- virtual ~QGeoRouteParserOsrmV5Private();
-
- QGeoRouteReply::Error parseReply(QList<QGeoRoute> &routes, QString &errorString, const QByteArray &reply) const override;
- QUrl requestUrl(const QGeoRouteRequest &request, const QString &prefix) const override;
-
- bool m_useServerText = false;
- QString m_accessToken;
-};
-
-QGeoRouteParserOsrmV5Private::QGeoRouteParserOsrmV5Private() : QGeoRouteParserPrivate()
-{
-}
-
-QGeoRouteParserOsrmV5Private::~QGeoRouteParserOsrmV5Private()
-{
-}
-
QGeoRouteReply::Error QGeoRouteParserOsrmV5Private::parseReply(QList<QGeoRoute> &routes, QString &errorString, const QByteArray &reply) const
{
// OSRM v5 specs: https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md
+ // Mapbox Directions API spec: https://www.mapbox.com/api-documentation/#directions
QJsonDocument document = QJsonDocument::fromJson(reply);
if (document.isObject()) {
QJsonObject object = document.object();
- QString status = object.value(QStringLiteral("code")).toString();
+ QString status = object.value(QLatin1String("code")).toString();
if (status != QLatin1String("Ok")) {
errorString = status;
return QGeoRouteReply::UnknownError;
@@ -934,7 +944,7 @@ QGeoRouteReply::Error QGeoRouteParserOsrmV5Private::parseReply(QList<QGeoRoute>
error = true;
break;
}
- QGeoRouteSegment segment = parseStep(s.toObject(), m_useServerText, legIndex, stepIndex);
+ QGeoRouteSegment segment = parseStep(s.toObject(), legIndex, stepIndex);
if (segment.isValid()) {
segments.append(segment);
} else {
@@ -969,7 +979,7 @@ QGeoRouteReply::Error QGeoRouteParserOsrmV5Private::parseReply(QList<QGeoRoute>
// setError(QGeoRouteReply::NoError, status); // can't do this, or NoError is emitted and does damages
return QGeoRouteReply::NoError;
} else {
- errorString = QStringLiteral("Couldn't parse json.");
+ errorString = QLatin1String("Couldn't parse json.");
return QGeoRouteReply::ParseError;
}
}
@@ -990,11 +1000,11 @@ QUrl QGeoRouteParserOsrmV5Private::requestUrl(const QGeoRouteRequest &request, c
routingUrl.append(QString::number(c.longitude(), 'f', 7)).append(QLatin1Char(',')).append(QString::number(c.latitude(), 'f', 7));
if (metadata.size() > i) {
const QVariantMap &meta = metadata.at(i);
- if (meta.contains(QStringLiteral("bearing"))) {
- qreal bearing = meta.value(QStringLiteral("bearing")).toDouble();
- bearings.append(QString::number(int(bearing))).append(QLatin1Char(',')).append(QStringLiteral("90")); // 90 is the angle of maneuver allowed.
+ if (meta.contains(QLatin1String("bearing"))) {
+ qreal bearing = meta.value(QLatin1String("bearing")).toDouble();
+ bearings.append(QString::number(int(bearing))).append(QLatin1Char(',')).append(QLatin1String("90")); // 90 is the angle of maneuver allowed.
} else {
- bearings.append(QStringLiteral("0,180")); // 180 here means anywhere
+ bearings.append(QLatin1String("0,180")); // 180 here means anywhere
}
}
++notFirst;
@@ -1002,31 +1012,31 @@ QUrl QGeoRouteParserOsrmV5Private::requestUrl(const QGeoRouteRequest &request, c
QUrl url(routingUrl);
QUrlQuery query;
- query.addQueryItem(QStringLiteral("overview"), QStringLiteral("full"));
- query.addQueryItem(QStringLiteral("steps"), QStringLiteral("true"));
- query.addQueryItem(QStringLiteral("geometries"), QStringLiteral("polyline6"));
- query.addQueryItem(QStringLiteral("alternatives"), QStringLiteral("true"));
- query.addQueryItem(QStringLiteral("bearings"), bearings);
- if (!m_accessToken.isEmpty())
- query.addQueryItem(QStringLiteral("access_token"), m_accessToken);
+ query.addQueryItem(QLatin1String("overview"), QLatin1String("full"));
+ query.addQueryItem(QLatin1String("steps"), QLatin1String("true"));
+ query.addQueryItem(QLatin1String("geometries"), QLatin1String("polyline6"));
+ query.addQueryItem(QLatin1String("alternatives"), QLatin1String("true"));
+ query.addQueryItem(QLatin1String("bearings"), bearings);
+ if (m_extension)
+ m_extension->updateQuery(query);
url.setQuery(query);
return url;
}
-QGeoRouteParserOsrmV5::QGeoRouteParserOsrmV5(QObject *parent, bool useServerText) : QGeoRouteParser(*new QGeoRouteParserOsrmV5Private(), parent)
+QGeoRouteParserOsrmV5::QGeoRouteParserOsrmV5(QObject *parent)
+ : QGeoRouteParser(*new QGeoRouteParserOsrmV5Private(), parent)
{
- Q_D(QGeoRouteParserOsrmV5);
- d->m_useServerText = useServerText;
}
QGeoRouteParserOsrmV5::~QGeoRouteParserOsrmV5()
{
}
-void QGeoRouteParserOsrmV5::setAccessToken(const QString &token)
+void QGeoRouteParserOsrmV5::setExtension(const QGeoRouteParserOsrmV5Extension *extension)
{
Q_D(QGeoRouteParserOsrmV5);
- d->m_accessToken = token;
+ if (extension)
+ d->m_extension = extension;
}
QT_END_NAMESPACE
diff --git a/src/location/maps/qgeorouteparserosrmv5_p.h b/src/location/maps/qgeorouteparserosrmv5_p.h
index 598dcde7..7f33af54 100644
--- a/src/location/maps/qgeorouteparserosrmv5_p.h
+++ b/src/location/maps/qgeorouteparserosrmv5_p.h
@@ -54,16 +54,32 @@
QT_BEGIN_NAMESPACE
class QGeoRouteParserOsrmV5Private;
+
+class Q_LOCATION_PRIVATE_EXPORT QGeoRouteParserOsrmV5Extension
+{
+public:
+ QGeoRouteParserOsrmV5Extension()
+ {
+ }
+
+ virtual ~QGeoRouteParserOsrmV5Extension()
+ {
+ }
+
+ virtual void updateQuery(QUrlQuery &query) const = 0;
+ virtual void updateSegment(QGeoRouteSegment &segment, const QJsonObject &step, const QJsonObject &maneuver) const = 0;
+};
+
class Q_LOCATION_PRIVATE_EXPORT QGeoRouteParserOsrmV5 : public QGeoRouteParser
{
Q_OBJECT
Q_DECLARE_PRIVATE(QGeoRouteParserOsrmV5)
public:
- QGeoRouteParserOsrmV5(QObject *parent = nullptr, bool useServerText = false);
+ QGeoRouteParserOsrmV5(QObject *parent = nullptr);
virtual ~QGeoRouteParserOsrmV5();
- void setAccessToken(const QString &token);
+ void setExtension(const QGeoRouteParserOsrmV5Extension *extension);
private:
Q_DISABLE_COPY(QGeoRouteParserOsrmV5)