59 if (instructionCode == QLatin1String(
"0"))
60 return QGeoManeuver::NoDirection;
61 else if (instructionCode == QLatin1String(
"1"))
62 return QGeoManeuver::DirectionForward;
63 else if (instructionCode == QLatin1String(
"2"))
64 return QGeoManeuver::DirectionBearRight;
65 else if (instructionCode == QLatin1String(
"3"))
66 return QGeoManeuver::DirectionRight;
67 else if (instructionCode == QLatin1String(
"4"))
68 return QGeoManeuver::DirectionHardRight;
69 else if (instructionCode == QLatin1String(
"5")) {
70 switch (trafficSide) {
71 case QGeoRouteParser::RightHandTraffic:
72 return QGeoManeuver::DirectionUTurnLeft;
73 case QGeoRouteParser::LeftHandTraffic:
74 return QGeoManeuver::DirectionUTurnRight;
76 return QGeoManeuver::DirectionUTurnLeft;
77 }
else if (instructionCode == QLatin1String(
"6"))
78 return QGeoManeuver::DirectionHardLeft;
79 else if (instructionCode == QLatin1String(
"7"))
80 return QGeoManeuver::DirectionLeft;
81 else if (instructionCode == QLatin1String(
"8"))
82 return QGeoManeuver::DirectionBearLeft;
83 else if (instructionCode == QLatin1String(
"9"))
84 return QGeoManeuver::NoDirection;
85 else if (instructionCode == QLatin1String(
"10"))
86 return QGeoManeuver::DirectionForward;
87 else if (instructionCode == QLatin1String(
"11"))
88 return QGeoManeuver::NoDirection;
89 else if (instructionCode == QLatin1String(
"12"))
90 return QGeoManeuver::NoDirection;
91 else if (instructionCode == QLatin1String(
"13"))
92 return QGeoManeuver::NoDirection;
93 else if (instructionCode == QLatin1String(
"14"))
94 return QGeoManeuver::NoDirection;
95 else if (instructionCode == QLatin1String(
"15"))
96 return QGeoManeuver::NoDirection;
98 return QGeoManeuver::NoDirection;
103 if (instructionCode == QLatin1String(
"0")) {
105 }
else if (instructionCode == QLatin1String(
"1")) {
106 if (wayname.isEmpty())
107 return QGeoRouteParserOsrmV4::tr(
"Go straight.");
109 return QGeoRouteParserOsrmV4::tr(
"Go straight onto %1.").arg(wayname);
110 }
else if (instructionCode == QLatin1String(
"2")) {
111 if (wayname.isEmpty())
112 return QGeoRouteParserOsrmV4::tr(
"Turn slightly right.");
114 return QGeoRouteParserOsrmV4::tr(
"Turn slightly right onto %1.").arg(wayname);
115 }
else if (instructionCode == QLatin1String(
"3")) {
116 if (wayname.isEmpty())
117 return QGeoRouteParserOsrmV4::tr(
"Turn right.");
119 return QGeoRouteParserOsrmV4::tr(
"Turn right onto %1.").arg(wayname);
120 }
else if (instructionCode == QLatin1String(
"4")) {
121 if (wayname.isEmpty())
122 return QGeoRouteParserOsrmV4::tr(
"Make a sharp right.");
124 return QGeoRouteParserOsrmV4::tr(
"Make a sharp right onto %1.").arg(wayname);
126 else if (instructionCode == QLatin1String(
"5")) {
127 return QGeoRouteParserOsrmV4::tr(
"When it is safe to do so, perform a U-turn.");
128 }
else if (instructionCode == QLatin1String(
"6")) {
129 if (wayname.isEmpty())
130 return QGeoRouteParserOsrmV4::tr(
"Make a sharp left.");
132 return QGeoRouteParserOsrmV4::tr(
"Make a sharp left onto %1.").arg(wayname);
133 }
else if (instructionCode == QLatin1String(
"7")) {
134 if (wayname.isEmpty())
135 return QGeoRouteParserOsrmV4::tr(
"Turn left.");
137 return QGeoRouteParserOsrmV4::tr(
"Turn left onto %1.").arg(wayname);
138 }
else if (instructionCode == QLatin1String(
"8")) {
139 if (wayname.isEmpty())
140 return QGeoRouteParserOsrmV4::tr(
"Turn slightly left.");
142 return QGeoRouteParserOsrmV4::tr(
"Turn slightly left onto %1.").arg(wayname);
143 }
else if (instructionCode == QLatin1String(
"9")) {
144 return QGeoRouteParserOsrmV4::tr(
"Reached waypoint.");
145 }
else if (instructionCode == QLatin1String(
"10")) {
146 if (wayname.isEmpty())
147 return QGeoRouteParserOsrmV4::tr(
"Head on.");
149 return QGeoRouteParserOsrmV4::tr(
"Head onto %1.").arg(wayname);
150 }
else if (instructionCode == QLatin1String(
"11")) {
151 return QGeoRouteParserOsrmV4::tr(
"Enter the roundabout.");
152 }
else if (instructionCode == QLatin1String(
"11-1")) {
153 if (wayname.isEmpty())
154 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the first exit.");
156 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the first exit onto %1.").arg(wayname);
157 }
else if (instructionCode == QLatin1String(
"11-2")) {
158 if (wayname.isEmpty())
159 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the second exit.");
161 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the second exit onto %1.").arg(wayname);
162 }
else if (instructionCode == QLatin1String(
"11-3")) {
163 if (wayname.isEmpty())
164 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the third exit.");
166 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the third exit onto %1.").arg(wayname);
167 }
else if (instructionCode == QLatin1String(
"11-4")) {
168 if (wayname.isEmpty())
169 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the fourth exit.");
171 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the fourth exit onto %1.").arg(wayname);
172 }
else if (instructionCode == QLatin1String(
"11-5")) {
173 if (wayname.isEmpty())
174 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the fifth exit.");
176 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the fifth exit onto %1.").arg(wayname);
177 }
else if (instructionCode == QLatin1String(
"11-6")) {
178 if (wayname.isEmpty())
179 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the sixth exit.");
181 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the sixth exit onto %1.").arg(wayname);
182 }
else if (instructionCode == QLatin1String(
"11-7")) {
183 if (wayname.isEmpty())
184 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the seventh exit.");
186 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the seventh exit onto %1.").arg(wayname);
187 }
else if (instructionCode == QLatin1String(
"11-8")) {
188 if (wayname.isEmpty())
189 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the eighth exit.");
191 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the eighth exit onto %1.").arg(wayname);
192 }
else if (instructionCode == QLatin1String(
"11-9")) {
193 if (wayname.isEmpty())
194 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the ninth exit.");
196 return QGeoRouteParserOsrmV4::tr(
"At the roundabout take the ninth exit onto %1.").arg(wayname);
197 }
else if (instructionCode == QLatin1String(
"12")) {
198 if (wayname.isEmpty())
199 return QGeoRouteParserOsrmV4::tr(
"Leave the roundabout.");
201 return QGeoRouteParserOsrmV4::tr(
"Leave the roundabout onto %1.").arg(wayname);
202 }
else if (instructionCode == QLatin1String(
"13")) {
203 return QGeoRouteParserOsrmV4::tr(
"Stay on the roundabout.");
204 }
else if (instructionCode == QLatin1String(
"14")) {
205 if (wayname.isEmpty())
206 return QGeoRouteParserOsrmV4::tr(
"Start at the end of the street.");
208 return QGeoRouteParserOsrmV4::tr(
"Start at the end of %1.").arg(wayname);
209 }
else if (instructionCode == QLatin1String(
"15")) {
210 return QGeoRouteParserOsrmV4::tr(
"You have reached your destination.");
212 return QGeoRouteParserOsrmV4::tr(
"Don't know what to say for '%1'").arg(instructionCode);
217 const QJsonObject &summary, QGeoRouteParser::TrafficSide trafficSide)
221 const QList<QGeoCoordinate> path = parsePolyline(geometry);
224 int firstPosition = -1;
226 for (qsizetype i = instructions.count() - 1; i >= 0; --i) {
227 const QJsonArray instruction = instructions.at(i).toArray();
229 if (instruction.count() < 8) {
230 qWarning(
"Instruction does not contain enough fields.");
234 const QString instructionCode = instruction.at(0).toString();
235 const QString wayname = instruction.at(1).toString();
236 double segmentLength = instruction.at(2).toDouble();
237 int position = instruction.at(3).toDouble();
238 int time = instruction.at(4).toDouble();
244 segment.setDistance(segmentLength);
246 QGeoManeuver maneuver;
247 maneuver.setDirection(osrmInstructionDirection(instructionCode, trafficSide));
248 maneuver.setDistanceToNextInstruction(segmentLength);
249 maneuver.setInstructionText(osrmInstructionText(instructionCode, wayname));
250 maneuver.setPosition(path.at(position));
251 maneuver.setTimeToNextInstruction(time);
253 segment.setManeuver(maneuver);
255 if (firstPosition == -1)
256 segment.setPath(path.mid(position));
258 segment.setPath(path.mid(position, firstPosition - position));
260 segment.setTravelTime(time);
262 segment.setNextRouteSegment(firstSegment);
264 firstSegment = segment;
265 firstPosition = position;
268 route.setDistance(summary.value(QStringLiteral(
"total_distance")).toDouble());
269 route.setTravelTime(summary.value(QStringLiteral(
"total_time")).toDouble());
270 route.setFirstRouteSegment(firstSegment);
298 QJsonDocument document = QJsonDocument::fromJson(reply);
300 if (document.isObject()) {
301 QJsonObject object = document.object();
304 int status = object.value(QStringLiteral(
"status")).toDouble();
305 QString statusMessage = object.value(QStringLiteral(
"status_message")).toString();
310 if (0 != status && 200 != status) {
311 errorString = statusMessage;
312 return QGeoRouteReply::UnknownError;
315 QJsonObject routeSummary = object.value(QStringLiteral(
"route_summary")).toObject();
317 QByteArray routeGeometry =
318 object.value(QStringLiteral(
"route_geometry")).toString().toLatin1();
320 QJsonArray routeInstructions = object.value(QStringLiteral(
"route_instructions")).toArray();
322 QGeoRoute route = constructRoute(routeGeometry, routeInstructions, routeSummary, trafficSide);
324 routes.append(route);
326 const QJsonArray alternativeSummaries =
327 object.value(QStringLiteral(
"alternative_summaries")).toArray();
328 const QJsonArray alternativeGeometries =
329 object.value(QStringLiteral(
"alternative_geometries")).toArray();
330 const QJsonArray alternativeInstructions =
331 object.value(QStringLiteral(
"alternative_instructions")).toArray();
333 if (alternativeSummaries.count() == alternativeGeometries.count() &&
334 alternativeSummaries.count() == alternativeInstructions.count()) {
335 for (qsizetype i = 0; i < alternativeSummaries.count(); ++i) {
336 route = constructRoute(alternativeGeometries.at(i).toString().toLatin1(),
337 alternativeInstructions.at(i).toArray(),
338 alternativeSummaries.at(i).toObject(),
344 return QGeoRouteReply::NoError;
346 errorString = QStringLiteral(
"Couldn't parse json.");
347 return QGeoRouteReply::ParseError;