buildInstruction method

String buildInstruction(
  1. RoadStep step,
  2. Map<String, dynamic> instructionsHelper,
  3. Map<String, dynamic> option
)
inherited

Implementation

String buildInstruction(
  RoadStep step,
  Map<String, dynamic> instructionsHelper,
  Map<String, dynamic> option,
) {
  var type = step.maneuver.maneuverType;
  final instructionsV5 = instructionsHelper['v5'] as Map<String, dynamic>;
  if (!instructionsV5.containsKey(type)) {
    type = 'turn';
  }

  var instructionObject = (instructionsV5[type]
      as Map<String, dynamic>)['default'] as Map<String, dynamic>;
  final omitSide = type == 'off ramp' &&
      ((step.maneuver.modifier?.indexOf(step.drivingSide) ?? 0) >= 0);
  if (step.maneuver.modifier != null &&
      (instructionsV5[type] as Map<String, dynamic>)
          .containsKey(step.maneuver.modifier!) &&
      !omitSide) {
    instructionObject = (instructionsV5[type]
            as Map<String, dynamic>)[step.maneuver.modifier!]
        as Map<String, dynamic>;
  }
  String? laneInstruction;
  switch (step.maneuver.maneuverType) {
    case 'use lane':
      final lane = laneConfig(step);
      if (lane != null) {
        laneInstruction = (((instructionsV5[type]
                as Map<String, dynamic>)['constants']
            as Map<String, dynamic>)['lanes'] as Map<String, String>)[lane];
      } else {
        instructionObject = ((instructionsV5[type]
                as Map<String, dynamic>)[step.maneuver.maneuverType]
            as Map<String, dynamic>)['no_lanes'] as Map<String, dynamic>;
      }
      break;
    case 'rotary':
    case 'roundabout':
      if (step.rotaryName != null &&
          step.maneuver.exit != null &&
          instructionObject.containsKey('name_exit')) {
        instructionObject =
            instructionObject['name_exit'] as Map<String, dynamic>;
      } else if (step.rotaryName != null &&
          instructionObject.containsKey('name')) {
        instructionObject = instructionObject['name'] as Map<String, dynamic>;
      } else if (step.maneuver.exit != null &&
          instructionObject.containsKey('exit')) {
        instructionObject = instructionObject['exit'] as Map<String, dynamic>;
      } else {
        instructionObject =
            instructionObject['default'] as Map<String, dynamic>;
      }
      break;
    default:
      break;
  }

  final name = retrieveName(step);
  var instruction = instructionObject['default'] as String;
  if (step.destinations != null &&
      step.exits != null &&
      instructionObject.containsKey('exit_destination')) {
    instruction = instructionObject['exit_destination'] as String;
  } else if (step.destinations != null &&
      instructionObject.containsKey('destination')) {
    instruction = instructionObject['destination'] as String;
  } else if (step.exits != null && instructionObject.containsKey('exit')) {
    instruction = instructionObject['exit'] as String;
  } else if (name.isNotEmpty && instructionObject.containsKey('name')) {
    instruction = instructionObject['name'] as String;
  } else if (option['waypointname'] != null &&
      instructionObject.containsKey('named')) {
    instruction = instructionObject['named'] as String;
  }
  var firstDestination = "";
  try {
    if (step.destinations != null) {
      var destinationSplits = step.destinations!.split(':');
      var destinationRef = destinationSplits.first.split(',').first;
      if (destinationSplits.length > 1) {
        var destination = destinationSplits[1].split(',').first;
        firstDestination = destination;
        if (destination.isNotEmpty && destinationRef.isNotEmpty) {
          firstDestination = "$destinationRef: $destination";
        } else {
          if (destination.isNotEmpty) {
            firstDestination = "$destinationRef: $destination";
          } else if (destinationRef.isNotEmpty) {
            firstDestination = destinationRef;
          }
        }
      } else {
        firstDestination = destinationRef;
      }
    }
  } catch (e) {
    debugPrint(e.toString());
  }
  String modifierInstruction = "";
  if (step.maneuver.modifier != null) {
    modifierInstruction =
        (instructionsV5["constants"] as Map<String, dynamic>)["modifier"]
            [step.maneuver.modifier] as String;
  }

  String nthWaypoint = "";
  if (option["legIndex"] != null &&
      option["legIndex"] != -1 &&
      option["legIndex"] != option["legCount"]) {
    String key = (option["legIndex"] + 1).toString();
    nthWaypoint = ordinalize(instructionsV5: instructionsV5, key: key);
  }

  String exitOrdinalise = "";
  if (step.maneuver.exit != null) {
    exitOrdinalise = ordinalize(
        instructionsV5: instructionsV5, key: step.maneuver.exit.toString());
  }

  return tokenize(instruction, {
    "way_name": name,
    "destination": firstDestination,
    "exit": step.exits?.split(",").first ?? "",
    "exit_number": exitOrdinalise,
    "rotary_name": step.rotaryName ?? "",
    "lane_instruction": laneInstruction ?? "",
    "modifier": modifierInstruction,
    "direction": directionFromDegree(step.maneuver.bearingBefore),
    "nth": nthWaypoint,
  });
}