diff options
author | Paolo Angelelli <paolo.angelelli@qt.io> | 2017-11-28 16:53:54 +0100 |
---|---|---|
committer | Paolo Angelelli <paolo.angelelli@qt.io> | 2017-11-29 23:02:27 +0000 |
commit | fe821df2179d496a3ffc853076e9c62f96abde2a (patch) | |
tree | 649ce88bd270af2a74b47ae6e529fc0ecd6122e0 /src/location/maps | |
parent | 057fe199941b8783312abeedde315118b59ccb8e (diff) | |
download | qtlocation-fe821df2179d496a3ffc853076e9c62f96abde2a.tar.gz |
Unify OSRM backends for OSM and Mapbox plugins
This patch unifies the OSRM backend in both the OSM and Mapbox plugins,
adding some extra functionalities to QGeoRouteParserOsrmV5 to handle
the extra osrm-text-instructions information coming from the Mapbox
servers.
It also adds a plugin parameter to let the user choose whether to
use the server's text instructions or the plugin-generated ones.
Change-Id: Id7ce73f4285e2e7db6872f40d72c0610847fce91
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'src/location/maps')
-rw-r--r-- | src/location/maps/qgeorouteparserosrmv5.cpp | 29 | ||||
-rw-r--r-- | src/location/maps/qgeorouteparserosrmv5_p.h | 4 |
2 files changed, 27 insertions, 6 deletions
diff --git a/src/location/maps/qgeorouteparserosrmv5.cpp b/src/location/maps/qgeorouteparserosrmv5.cpp index 75daefda..38eeb243 100644 --- a/src/location/maps/qgeorouteparserosrmv5.cpp +++ b/src/location/maps/qgeorouteparserosrmv5.cpp @@ -793,8 +793,10 @@ static QGeoManeuver::InstructionDirection instructionDirection(const QJsonObject return QGeoManeuver::NoDirection; } -static QGeoRouteSegment parseStep(const QJsonObject &step) { - // OSRM Instructions documentation: https://github.com/Project-OSRM/osrm-text-instructions/blob/master/instructions.json +static QGeoRouteSegment parseStep(const QJsonObject &step, bool useServerText) { + // 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". QGeoRouteSegment segment; if (!step.value(QLatin1String("maneuver")).isObject()) return segment; @@ -808,6 +810,10 @@ static QGeoRouteSegment parseStep(const QJsonObject &step) { 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(); @@ -825,7 +831,7 @@ static QGeoRouteSegment parseStep(const QJsonObject &step) { geoManeuver.setDirection(instructionDirection(maneuver)); geoManeuver.setDistanceToNextInstruction(distance); geoManeuver.setTimeToNextInstruction(time); - geoManeuver.setInstructionText(instructionText(step, maneuver, geoManeuver.direction())); + geoManeuver.setInstructionText((useServerText && !instruction_text.isEmpty()) ? instruction_text : instructionText(step, maneuver, geoManeuver.direction())); geoManeuver.setPosition(coord); geoManeuver.setWaypoint(coord); @@ -845,6 +851,9 @@ public: QGeoRouteReply::Error parseReply(QList<QGeoRoute> &routes, QString &errorString, const QByteArray &reply) const Q_DECL_OVERRIDE; QUrl requestUrl(const QGeoRouteRequest &request, const QString &prefix) const Q_DECL_OVERRIDE; + + bool m_useServerText = false; + QString m_accessToken; }; QGeoRouteParserOsrmV5Private::QGeoRouteParserOsrmV5Private() : QGeoRouteParserPrivate() @@ -906,7 +915,7 @@ QGeoRouteReply::Error QGeoRouteParserOsrmV5Private::parseReply(QList<QGeoRoute> error = true; break; } - QGeoRouteSegment segment = parseStep(s.toObject()); + QGeoRouteSegment segment = parseStep(s.toObject(), m_useServerText); if (segment.isValid()) { segments.append(segment); } else { @@ -963,16 +972,26 @@ QUrl QGeoRouteParserOsrmV5Private::requestUrl(const QGeoRouteRequest &request, c query.addQueryItem(QStringLiteral("steps"), QStringLiteral("true")); query.addQueryItem(QStringLiteral("geometries"), QStringLiteral("polyline")); query.addQueryItem(QStringLiteral("alternatives"), QStringLiteral("true")); + if (!m_accessToken.isEmpty()) + query.addQueryItem(QStringLiteral("access_token"), m_accessToken); url.setQuery(query); return url; } -QGeoRouteParserOsrmV5::QGeoRouteParserOsrmV5(QObject *parent) : QGeoRouteParser(*new QGeoRouteParserOsrmV5Private(), parent) +QGeoRouteParserOsrmV5::QGeoRouteParserOsrmV5(QObject *parent, bool useServerText) : QGeoRouteParser(*new QGeoRouteParserOsrmV5Private(), parent) { + Q_D(QGeoRouteParserOsrmV5); + d->m_useServerText = useServerText; } QGeoRouteParserOsrmV5::~QGeoRouteParserOsrmV5() { } +void QGeoRouteParserOsrmV5::setAccessToken(const QString &token) +{ + Q_D(QGeoRouteParserOsrmV5); + d->m_accessToken = token; +} + QT_END_NAMESPACE diff --git a/src/location/maps/qgeorouteparserosrmv5_p.h b/src/location/maps/qgeorouteparserosrmv5_p.h index d2c59165..c6ad0367 100644 --- a/src/location/maps/qgeorouteparserosrmv5_p.h +++ b/src/location/maps/qgeorouteparserosrmv5_p.h @@ -60,9 +60,11 @@ class Q_LOCATION_PRIVATE_EXPORT QGeoRouteParserOsrmV5 : public QGeoRouteParser Q_DECLARE_PRIVATE(QGeoRouteParserOsrmV5) public: - QGeoRouteParserOsrmV5(QObject *parent = Q_NULLPTR); + QGeoRouteParserOsrmV5(QObject *parent = Q_NULLPTR, bool useServerText = false); virtual ~QGeoRouteParserOsrmV5(); + void setAccessToken(const QString &token); + private: Q_DISABLE_COPY(QGeoRouteParserOsrmV5) }; |