58 if (instructionCode == QLatin1String(
"0"))
59 return QGeoManeuver::NoDirection;
60 else if (instructionCode == QLatin1String(
"1"))
61 return QGeoManeuver::DirectionForward;
62 else if (instructionCode == QLatin1String(
"2"))
63 return QGeoManeuver::DirectionBearRight;
64 else if (instructionCode == QLatin1String(
"3"))
65 return QGeoManeuver::DirectionRight;
66 else if (instructionCode == QLatin1String(
"4"))
67 return QGeoManeuver::DirectionHardRight;
68 else if (instructionCode == QLatin1String(
"5")) {
69 switch (trafficSide) {
70 case QGeoRouteParser::RightHandTraffic:
71 return QGeoManeuver::DirectionUTurnLeft;
72 case QGeoRouteParser::LeftHandTraffic:
73 return QGeoManeuver::DirectionUTurnRight;
75 return QGeoManeuver::DirectionUTurnLeft;
76 }
else if (instructionCode == QLatin1String(
"6"))
77 return QGeoManeuver::DirectionHardLeft;
78 else if (instructionCode == QLatin1String(
"7"))
79 return QGeoManeuver::DirectionLeft;
80 else if (instructionCode == QLatin1String(
"8"))
81 return QGeoManeuver::DirectionBearLeft;
82 else if (instructionCode == QLatin1String(
"9"))
83 return QGeoManeuver::NoDirection;
84 else if (instructionCode == QLatin1String(
"10"))
85 return QGeoManeuver::DirectionForward;
86 else if (instructionCode == QLatin1String(
"11"))
87 return QGeoManeuver::NoDirection;
88 else if (instructionCode == QLatin1String(
"12"))
89 return QGeoManeuver::NoDirection;
90 else if (instructionCode == QLatin1String(
"13"))
91 return QGeoManeuver::NoDirection;
92 else if (instructionCode == QLatin1String(
"14"))
93 return QGeoManeuver::NoDirection;
94 else if (instructionCode == QLatin1String(
"15"))
95 return QGeoManeuver::NoDirection;
97 return QGeoManeuver::NoDirection;
102 if (instructionCode == QLatin1String(
"0")) {
104 }
else if (instructionCode == QLatin1String(
"1")) {
105 if (wayname.isEmpty())
106 return QGeoRouteParserOsrmV4::tr(
"Go straight.");
108 return QGeoRouteParserOsrmV4::tr(
"Go straight onto %1.").arg(wayname);
109 }
else if (instructionCode == QLatin1String(
"2")) {
110 if (wayname.isEmpty())
111 return QGeoRouteParserOsrmV4::tr(
"Turn slightly right.");
113 return QGeoRouteParserOsrmV4::tr(
"Turn slightly right onto %1.").arg(wayname);
114 }
else if (instructionCode == QLatin1String(
"3")) {
115 if (wayname.isEmpty())
116 return QGeoRouteParserOsrmV4::tr(
"Turn right.");
118 return QGeoRouteParserOsrmV4::tr(
"Turn right onto %1.").arg(wayname);
119 }
else if (instructionCode == QLatin1String(
"4")) {
120 if (wayname.isEmpty())
121 return QGeoRouteParserOsrmV4::tr(
"Make a sharp right.");
123 return QGeoRouteParserOsrmV4::tr(
"Make a sharp right onto %1.").arg(wayname);
125 else if (instructionCode == QLatin1String(
"5")) {
126 return QGeoRouteParserOsrmV4::tr(
"When it is safe to do so, perform a U-turn.");
127 }
else if (instructionCode == QLatin1String(
"6")) {
128 if (wayname.isEmpty())
129 return QGeoRouteParserOsrmV4::tr(
"Make a sharp left.");
131 return QGeoRouteParserOsrmV4::tr(
"Make a sharp left onto %1.").arg(wayname);
132 }
else if (instructionCode == QLatin1String(
"7")) {
133 if (wayname.isEmpty())
134 return QGeoRouteParserOsrmV4::tr(
"Turn left.");
136 return QGeoRouteParserOsrmV4::tr(
"Turn left onto %1.").arg(wayname);
137 }
else if (instructionCode == QLatin1String(
"8")) {
138 if (wayname.isEmpty())
139 return QGeoRouteParserOsrmV4::tr(
"Turn slightly left.");
141 return QGeoRouteParserOsrmV4::tr(
"Turn slightly left onto %1.").arg(wayname);
142 }
else if (instructionCode == QLatin1String(
"9")) {
143 return QGeoRouteParserOsrmV4::tr(
"Reached waypoint.");
144 }
else if (instructionCode == QLatin1String(
"10")) {
145 if (wayname.isEmpty())
146 return QGeoRouteParserOsrmV4::tr(
"Head on.");
148 return QGeoRouteParserOsrmV4::tr(
"Head onto %1.").arg(wayname);
149 }
else if (instructionCode == QLatin1String(
"11")) {
150 return QGeoRouteParserOsrmV4::tr(
"Enter the roundabout.");
151 }
else if (instructionCode == QLatin1String(
"11-1")) {
152 if (wayname.isEmpty())
153 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the first exit.");
155 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the first exit onto %1.").arg(wayname);
156 }
else if (instructionCode == QLatin1String(
"11-2")) {
157 if (wayname.isEmpty())
158 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the second exit.");
160 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the second exit onto %1.").arg(wayname);
161 }
else if (instructionCode == QLatin1String(
"11-3")) {
162 if (wayname.isEmpty())
163 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the third exit.");
165 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the third exit onto %1.").arg(wayname);
166 }
else if (instructionCode == QLatin1String(
"11-4")) {
167 if (wayname.isEmpty())
168 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the fourth exit.");
170 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the fourth exit onto %1.").arg(wayname);
171 }
else if (instructionCode == QLatin1String(
"11-5")) {
172 if (wayname.isEmpty())
173 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the fifth exit.");
175 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the fifth exit onto %1.").arg(wayname);
176 }
else if (instructionCode == QLatin1String(
"11-6")) {
177 if (wayname.isEmpty())
178 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the sixth exit.");
180 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the sixth exit onto %1.").arg(wayname);
181 }
else if (instructionCode == QLatin1String(
"11-7")) {
182 if (wayname.isEmpty())
183 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the seventh exit.");
185 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the seventh exit onto %1.").arg(wayname);
186 }
else if (instructionCode == QLatin1String(
"11-8")) {
187 if (wayname.isEmpty())
188 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the eighth exit.");
190 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the eighth exit onto %1.").arg(wayname);
191 }
else if (instructionCode == QLatin1String(
"11-9")) {
192 if (wayname.isEmpty())
193 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the ninth exit.");
195 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the ninth exit onto %1.").arg(wayname);
196 }
else if (instructionCode == QLatin1String(
"12")) {
197 if (wayname.isEmpty())
198 return QGeoRouteParserOsrmV4::tr(
"Leave the roundabout.");
200 return QGeoRouteParserOsrmV4::tr(
"Leave the roundabout onto %1.").arg(wayname);
201 }
else if (instructionCode == QLatin1String(
"13")) {
202 return QGeoRouteParserOsrmV4::tr(
"Stay on the roundabout.");
203 }
else if (instructionCode == QLatin1String(
"14")) {
204 if (wayname.isEmpty())
205 return QGeoRouteParserOsrmV4::tr(
"Start at the end of the street.");
207 return QGeoRouteParserOsrmV4::tr(
"Start at the end of %1.").arg(wayname);
208 }
else if (instructionCode == QLatin1String(
"15")) {
209 return QGeoRouteParserOsrmV4::tr(
"You have reached your destination.");
211 return QGeoRouteParserOsrmV4::tr(
"Don't know what to say for '%1'").arg(instructionCode);
216 const QJsonObject &summary, QGeoRouteParser::TrafficSide trafficSide)
220 const QList<QGeoCoordinate> path = parsePolyline(geometry);
223 int firstPosition = -1;
225 for (qsizetype i = instructions.count() - 1; i >= 0; --i) {
226 const QJsonArray instruction = instructions.at(i).toArray();
228 if (instruction.count() < 8) {
229 qWarning(
"Instruction does not contain enough fields.");
233 const QString instructionCode = instruction.at(0).toString();
234 const QString wayname = instruction.at(1).toString();
235 double segmentLength = instruction.at(2).toDouble();
236 int position = instruction.at(3).toDouble();
237 int time = instruction.at(4).toDouble();
243 segment.setDistance(segmentLength);
245 QGeoManeuver maneuver;
246 maneuver.setDirection(osrmInstructionDirection(instructionCode, trafficSide));
247 maneuver.setDistanceToNextInstruction(segmentLength);
248 maneuver.setInstructionText(osrmInstructionText(instructionCode, wayname));
249 maneuver.setPosition(path.at(position));
250 maneuver.setTimeToNextInstruction(time);
252 segment.setManeuver(maneuver);
254 if (firstPosition == -1)
255 segment.setPath(path.mid(position));
257 segment.setPath(path.mid(position, firstPosition - position));
259 segment.setTravelTime(time);
261 segment.setNextRouteSegment(firstSegment);
263 firstSegment = segment;
264 firstPosition = position;
267 route.setDistance(summary.value(QStringLiteral(
"total_distance")).toDouble());
268 route.setTravelTime(summary.value(QStringLiteral(
"total_time")).toDouble());
269 route.setFirstRouteSegment(firstSegment);
297 QJsonDocument document = QJsonDocument::fromJson(reply);
299 if (document.isObject()) {
300 QJsonObject object = document.object();
303 int status = object.value(QStringLiteral(
"status")).toDouble();
304 QString statusMessage = object.value(QStringLiteral(
"status_message")).toString();
309 if (0 != status && 200 != status) {
310 errorString = statusMessage;
311 return QGeoRouteReply::UnknownError;
314 QJsonObject routeSummary = object.value(QStringLiteral(
"route_summary")).toObject();
316 QByteArray routeGeometry =
317 object.value(QStringLiteral(
"route_geometry")).toString().toLatin1();
319 QJsonArray routeInstructions = object.value(QStringLiteral(
"route_instructions")).toArray();
321 QGeoRoute route = constructRoute(routeGeometry, routeInstructions, routeSummary, trafficSide);
323 routes.append(route);
325 const QJsonArray alternativeSummaries =
326 object.value(QStringLiteral(
"alternative_summaries")).toArray();
327 const QJsonArray alternativeGeometries =
328 object.value(QStringLiteral(
"alternative_geometries")).toArray();
329 const QJsonArray alternativeInstructions =
330 object.value(QStringLiteral(
"alternative_instructions")).toArray();
332 if (alternativeSummaries.count() == alternativeGeometries.count() &&
333 alternativeSummaries.count() == alternativeInstructions.count()) {
334 for (qsizetype i = 0; i < alternativeSummaries.count(); ++i) {
335 route = constructRoute(alternativeGeometries.at(i).toString().toLatin1(),
336 alternativeInstructions.at(i).toArray(),
337 alternativeSummaries.at(i).toObject(),
343 return QGeoRouteReply::NoError;
345 errorString = QStringLiteral(
"Couldn't parse json.");
346 return QGeoRouteReply::ParseError;