nextInstruction method

Future<TurnByTurnInformation?> nextInstruction(
  1. List<RoadInstruction> instructions,
  2. Road road,
  3. LngLat currentLocation, {
  4. double tolerance = 0.1,
})

nextInstruction

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

Future<TurnByTurnInformation?> nextInstruction(
  List<RoadInstruction> instructions,
  Road road,
  LngLat currentLocation, {
  double tolerance = 0.1,
}) async {
  var polyline = road.polyline;
  if (road.polyline == null && road.polylineEncoded == null) {
    throw Exception(
        'we cannot provide next instruction where [polylines] or/and [polylineEncoded]  in roads is null');
  } else if (road.polyline == null && road.polylineEncoded != null) {
    polyline = PrivateRoad.decodePoylinesGeometry(road.polylineEncoded!);
  }
  final location = currentLocation.alignWithPrecision(precision: 5);

  final indexOfNextLocation = indexOfLocationFromRoad(
    location,
    polyline!,
    tolerance: tolerance,
  );
  if (indexOfNextLocation == -1 ||
      indexOfNextLocation > polyline.length - 1) {
    return null;
  }
  final nextLocation = polyline[indexOfNextLocation];

  var currentInstruction = instructions.cast<RoadInstruction?>().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),
  );
}