getCountriesFromLatLng function

Future<List<String>> getCountriesFromLatLng(
  1. LatLng point,
  2. int comparator(
    1. String a,
    2. String b
    )
)

Get a list of countries that ordered by the distance from the point @param {LatLng} point @returns {Future<String[]>}

Implementation

Future<List<String>> getCountriesFromLatLng(LatLng point, final int Function(String a, String b) comparator) async {
  var countries = _corners.keys.toList();
  Map<String, double> dist0 = {};
  for (var country in countries) {
    var c = _corners[country]!;
    if (c.isEmpty) {
      dist0[country] = double.infinity;
      continue;
    }
    if (point.lat > c[0] != point.lat > c[2] && point.lng > c[1] != point.lng > c[3]) {
      dist0[country] = 0;
    } else {
      var dx = min((point.lat - c[0]).abs(), (point.lat - c[2]).abs());
      var dy = min((point.lng - c[1]).abs(), (point.lng - c[3]).abs());
      dist0[country] = sqrt(dx * dx + dy * dy);
    }
  }
  countries.sort((a, b) => dist0[a]!.compareTo(dist0[b]!));
  int topCount = 0;
  while (topCount < countries.length && dist0[countries[topCount]]! == 0) {
    topCount++;
  }
  if (topCount < _verifyCountriesCount) {
    topCount = _verifyCountriesCount;
  }
  var top = countries.sublist(0, topCount);
  Map<String, GeoRelation> dist = {};
  for (var country in top) {
    dist[country] = await isInsideCountry(point, country);
  }
  top.sort((a, b) =>
      dist[a]!.inside != dist[b]!.inside ? (dist[a]!.inside ? -1 : 1) : dist[a]!.distance.compareTo(dist[b]!.distance));
  var remains = countries.sublist(topCount);
  remains.sort(comparator);
  top.addAll(remains);
  return top;
}