getLocation method

Future<LocationData> getLocation({
  1. CRS crs = CRS.gcj02,
})

get the current location

Implementation

Future<LocationData> getLocation({CRS crs = CRS.gcj02}) async {
  LocationData location = LocationData();

  if (await hasPermission() == true) {
    Completer c = Completer();

    /// 哪个先返回有效结果就用哪个
    for (var provider in providers) {
      provider.getLocation().then((value) {
        if (value.latitude != 0 && value.longitude != 0) {
          if (c.isCompleted != true) {
            c.complete(value);
          }
        }
      }).catchError((e) {
        print(e);
      });
    }

    try {
      location = await c.future;
    } catch (e) {
      print(e);
      location = await GeolocatorCNProviders.ip.getLocation();
    }
  } else {
    /// if we cat't get permission, we can only use the ip location api
    location = await GeolocatorCNProviders.ip.getLocation();
  }

  /// transform the location to the specified crs and return
  LocationData ret = _transormCrs(location, location.crs, crs);
  print('GeolocatorCN->getLocation: $ret');

  return ret;
}