regionsContaining method

List<RegionFeature> regionsContaining({
  1. double? lon,
  2. double? lat,
  3. List<double>? bbox,
  4. dynamic query,
  5. bool strict = false,
})

Implementation

List<RegionFeature> regionsContaining(
    {double? lon,
    double? lat,
    List<double>? bbox,
    dynamic query,
    bool strict = false}) {
  List<RegionFeature> matching;
  if (bbox != null) {
    assert(bbox.length == 4);
    matching = smallestRegionsForBBox(bbox[0], bbox[1], bbox[2], bbox[3]);
  } else if (lon != null && lat != null) {
    final region = smallestRegion(lon, lat);
    matching = region == null ? [] : [region];
  } else if (query != null) {
    final region = regionForID(query);
    matching = region == null ? [] : [region];
  } else {
    throw ArgumentError('Please specify either location, bbox or query.');
  }

  if (matching.isEmpty) return matching;

  List<RegionFeature> result;

  if (!strict || lat != null) {
    result = List.of(matching);
  } else {
    result = [];
  }

  for (final region in matching) {
    for (final groupId in region.groups) {
      final groupFeature = regionsByCode[groupId]!;
      if (!result.contains(groupFeature)) {
        result.add(groupFeature);
      }
    }
  }

  return result;
}