request method

Future<IsochroneApiResponse> request({
  1. NavigationProfile? profile,
  2. List<double> coordinates = const <double>[],
  3. List<int> contoursMinutes = const <int>[],
  4. List<String> contoursColors = const <String>[],
  5. bool polygons = false,
  6. double denoise = 1.0,
  7. double? generalize,
})

Given a location and a routing profile, retrieve up to four isochrone contours. The contours are calculated using rasters and are returned as either polygon or line features, depending on your input setting for the polygons parameter.

Implementation

Future<IsochroneApiResponse> request({
  NavigationProfile? profile,
  List<double> coordinates = const <double>[],
  List<int> contoursMinutes = const <int>[],
  List<String> contoursColors = const <String>[],
  bool polygons = false,
  double denoise = 1.0,
  double? generalize,
}) 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;
    }
  }

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

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

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

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

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

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

    url += contoursColors[i];

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

  if (polygons) {
    url += '&polygons=true';
  }

  if (denoise != 1.0) {
    url += '&denoise=';
    url += denoise.toString();
  }

  if (generalize != null) {
    url += '&generalize=';
    url += generalize.toString();
  }

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