drawRoute method

Future<void> drawRoute(
  1. List<LatLng> points,
  2. String routeName,
  3. Color routeColor,
  4. String googleApiKey, {
  5. TravelModes? travelMode,
})

Function that creates the actual route between multiple points

Implementation

Future<void> drawRoute(List<LatLng> points, String routeName,
    Color routeColor, String googleApiKey,
    {TravelModes? travelMode}) async {
  var previousPoint;
  TravelMode travelType;

  /// Checks which travel mode is defined in the parameters
  if (travelMode != null) {
    switch (travelMode) {
      case TravelModes.driving:
        travelType = TravelMode.driving;
        break;
      case TravelModes.bicycling:
        travelType = TravelMode.bicycling;
        break;
      case TravelModes.transit:
        travelType = TravelMode.transit;
        break;
      case TravelModes.walking:
        travelType = TravelMode.walking;
        break;
      default:
        travelType = TravelMode.driving;
    }
  }

  /// If the travel mode is not defined, it uses the default travel mode
  else {
    travelType = TravelMode.driving;
  }

  /// Iterates over the points and creates a route between each pair
  for (var i = 0; i < points.length; i++) {
    var point = points[i];

    /// If the previous point is null it creates a route
    /// from the first and second point
    if (previousPoint == null) {
      var nextPoint = points[i + 1];
      previousPoint = point;
      await _createRouteFragment(
          point.latitude,
          point.longitude,
          nextPoint.latitude,
          nextPoint.longitude,
          '${_replaceWhiteSpaces(routeName)}+$i',
          routeColor,
          googleApiKey,
          travelType);
    }

    /// If the previous point is not null it creates a route
    /// between the previous and current point
    else {
      await _createRouteFragment(
          previousPoint.latitude,
          previousPoint.longitude,
          point.latitude,
          point.longitude,
          '${_replaceWhiteSpaces(routeName)}+$i',
          routeColor,
          googleApiKey,
          travelType);
    }
  }
}