nextInstruction method
this method will provide the TurnByTurnInformation from currentLocation
,instructions
and road
which contain current instruction,next instruction and distance between currentLocation
and nextInstruction.location
the distance will in meters
Note that tolerance
can effect the result which used to mesure location in road which close to currentLocation
return Future of TurnByTurnInformation, can be null also if current location not in Path
Implementation
TurnByTurnInformation? nextInstruction(
List<RouteInstruction> instructions,
Route road,
LngLat currentLocation, {
double tolerance = 0.1,
int precision = 5,
}) {
var polyline = road.polyline;
if (road.polyline.isNullOrEmpty && road.polylineEncoded == null) {
throw Exception(
'we cannot provide next instruction where [polylines] or/and [polylineEncoded] in roads is null',
);
} else if (road.polyline.isNullOrEmpty && road.polylineEncoded != null) {
polyline = road.polylineEncoded!.decodeGeometry(precision: precision);
}
final location = currentLocation.alignWithPrecision(precision: precision);
final indexOfNextLocation = indexOfLocationFromRoad(
location,
polyline!,
tolerance: tolerance,
);
if (indexOfNextLocation == -1 ||
indexOfNextLocation > polyline.length - 1) {
return null;
}
final nextLocation = polyline[indexOfNextLocation];
var currentInstruction = instructions.cast<RouteInstruction?>().firstWhere(
(element) => element != null && element.location == nextLocation,
orElse: () => null);
currentInstruction ??=
closeInstructionToLocation(instructions, nextLocation);
if (currentInstruction == null) {
return null;
}
final nextInstruction =
instructions[instructions.indexOf(currentInstruction) + 1];
return (
currentInstruction: currentInstruction,
nextInstruction: nextInstruction,
distance: location.distance(location: nextInstruction.location),
);
}