request method

Future<MatrixApiResponse> request({
  1. NavigationProfile? profile,
  2. List<List<double>> coordinates = const <List<double>>[],
  3. List<NavigationAnnotations> annotations = const <NavigationAnnotations>[],
  4. List<NavigationApproaches> approaches = const <NavigationApproaches>[],
  5. List<int>? destinations,
  6. int fallbackSpeed = 0,
  7. List<int> sources = const <int>[],
})

Returns a duration matrix, a distance matrix, or both, showing travel times and distances between coordinates. In the default case, this endpoint returns a symmetric matrix that uses all the input coordinates as sources and destinations. Using the optional sources and destination parameters, you can also generate an asymmetric matrix that uses only some coordinates as sources or destinations.

Implementation

Future<MatrixApiResponse> request({
  NavigationProfile? profile,
  List<List<double>> coordinates = const <List<double>>[],
  List<NavigationAnnotations> annotations = const <NavigationAnnotations>[],
  List<NavigationApproaches> approaches = const <NavigationApproaches>[],
  List<int>? destinations,
  int fallbackSpeed = 0,
  List<int> sources = const <int>[],
}) async {
  var url = endpoint + '/' + version;

  if (profile != null) {
    switch (profile) {
      case NavigationProfile.DRIVING_TRAFFIC:
        url += '/mapbox/driving-traffic';
        break;
      case NavigationProfile.DRIVING:
        url += '/mapbox/driving';
        break;
      case NavigationProfile.CYCLING:
        url += '/mapbox/cycling';
        break;
      case NavigationProfile.WALKING:
        url += '/mapbox/walking';
        break;
    }
  }

  for (var i = 0; i < coordinates.length; i++) {
    if (i == 0) {
      url += '/';
    }

    url += coordinates[i][LONGITUDE].toString();
    url += ',';
    url += coordinates[i][LATITUDE].toString();

    if (i != coordinates.length - 1) {
      url += ';';
    }
  }

  url += '?access_token=' + api.accessToken!;

  for (var i = 0; i < annotations.length; i++) {
    if (i == 0) {
      url += '&annotations=';
    }

    switch (annotations[i]) {
      case NavigationAnnotations.DURATION:
        url += 'duration';
        break;
      case NavigationAnnotations.DISTANCE:
        url += 'distance';
        break;
      case NavigationAnnotations.SPEED:
      case NavigationAnnotations.CONGESTION:
        // not supported
        break;
    }

    if (i != annotations.length - 1) {
      url += ',';
    }
  }

  if (approaches.isNotEmpty) {
    for (var i = 0; i < approaches.length; i++) {
      if (i == 0) {
        url += '&approaches=';
      }

      switch (approaches[i]) {
        case NavigationApproaches.CURB:
          url += 'curb';
          break;
        case NavigationApproaches.UNRESTRICTED:
          url += 'unrestricted';
          break;
      }

      if (i != approaches.length - 1) {
        url += ';';
      }
    }
  }

  for (var i = 0; i < destinations!.length; i++) {
    if (i == 0) {
      url += '&destinations=';
    }

    url += destinations[i].toString();

    if (i != destinations.length - 1) {
      url += ',';
    }
  }

  for (var i = 0; i < sources.length; i++) {
    if (i == 0) {
      url += '&sources=';
    }

    url += sources[i].toString();

    if (i != sources.length - 1) {
      url += ',';
    }
  }

  if (fallbackSpeed != 0) {
    url += '&fallback_speed=';
    url += fallbackSpeed.toString();
  }

  try {
    final response = await get(Uri.parse(url));
    final json = jsonDecode(
      response.body,
    ) as Map<String, dynamic>;
    return MatrixApiResponse.fromJson(json);
  } on Error catch (error) {
    return MatrixApiResponse.withError(error);
  }
}