fetchLocation method

Future<Location> fetchLocation({
  1. LocationAccuracy mode = LocationAccuracy.Low,
  2. bool? needAddress,
  3. Duration? timeout,
})

单次获取定位信息

选择定位模式mode, 设置定位同时是否需要返回地址描述needAddress, 设置定位请求超时时间,默认为30秒timeout.

Implementation

Future<Location> fetchLocation({
  LocationAccuracy mode = LocationAccuracy.Low,
  bool? needAddress,
  Duration? timeout,
}) async {
  var completer = Completer<Location>();
  return platform(
    android: (pool) async {
      assert(_androidClient != null,
          '请先在main方法中调用AmapLocation.instance.init()进行初始化!');

      final listener =
          await com_amap_api_location_AMapLocationListener.anonymous__();
      listener.onLocationChanged = (location) async {
        if (!completer.isCompleted) {
          completer.complete(
            Location(
              address: await location!.getAddress(),
              latLng: LatLng(
                await location.getLatitude() ?? 0,
                await location.getLongitude() ?? 0,
              ),
              altitude: await location.getAltitude(),
              bearing: await location.getBearing(),
              country: await location.getCountry(),
              province: await location.getProvince(),
              city: await location.getCity(),
              cityCode: await location.getCityCode(),
              adCode: await location.getAdCode(),
              district: await location.getDistrict(),
              poiName: await location.getPoiName(),
              street: await location.getStreet(),
              streetNumber: await location.getStreetNum(),
              aoiName: await location.getAoiName(),
              accuracy: await location.getAccuracy(),
              speed: await location.speed,
            ),
          );
        }
      };
      await _androidClient?.setLocationListener(listener);

      // 创建选项
      final options =
          await com_amap_api_location_AMapLocationClientOption.create__();
      // 设置单次定位
      await options.setOnceLocation(true);
      // 设置定位模式
      switch (mode) {
        // 高精度定位模式:会同时使用网络定位和GPS定位,优先返回最高精度的定位结果,以及对应的地址描述信息。
        case LocationAccuracy.High:
          await options.setLocationMode(
              com_amap_api_location_AMapLocationClientOption_AMapLocationMode
                  .Hight_Accuracy);
          break;
        // 低功耗定位模式:不会使用GPS和其他传感器,只会使用网络定位(Wi-Fi和基站定位);
        case LocationAccuracy.Low:
          await options.setLocationMode(
              com_amap_api_location_AMapLocationClientOption_AMapLocationMode
                  .Battery_Saving);
          break;
        case LocationAccuracy.DeviceSensor:
          await options.setLocationMode(
              com_amap_api_location_AMapLocationClientOption_AMapLocationMode
                  .Device_Sensors);
          break;
      }
      // 是否返回地址描述
      if (needAddress != null) await options.setNeedAddress(needAddress);
      // 设置定位请求超时时间,默认为30秒。
      if (timeout != null) {
        await options.setHttpTimeOut(timeout.inMilliseconds);
      }

      await options.setSensorEnable(true);

      // 设置选项
      await _androidClient?.setLocationOption(options);

      // 开始定位
      await _androidClient?.startLocation();

      return completer.future;
    },
    ios: (pool) async {
      assert(_iosClient != null,
          '请先在main方法中调用AmapLocation.instance.init()进行初始化!');
      // 设置定位模式
      switch (mode) {
        // 高精度定位模式:会同时使用网络定位和GPS定位,优先返回最高精度的定位结果,以及对应的地址描述信息。
        case LocationAccuracy.High:
          await _iosClient?.set_desiredAccuracy(10);
          break;
        // 低功耗定位模式:不会使用GPS和其他传感器,只会使用网络定位(Wi-Fi和基站定位);
        case LocationAccuracy.DeviceSensor:
        case LocationAccuracy.Low:
          await _iosClient?.set_desiredAccuracy(100);
          break;
      }
      // 设置定位请求超时时间,默认为30秒。
      if (timeout != null) {
        await _iosClient?.set_locationTimeout(timeout.inSeconds);
      }

      await _iosClient?.requestLocationWithReGeocode_completionBlock(
        needAddress ?? true,
        (location, regeocode, error) async {
          if (!completer.isCompleted) {
            final latitude =
                await location!.coordinate.then((it) => it.latitude);
            final longitude =
                await location.coordinate.then((it) => it.longitude);
            completer.complete(Location(
              address: await regeocode?.get_formattedAddress(),
              latLng: LatLng(await latitude ?? 0, await longitude ?? 0),
              altitude: await location.altitude,
              bearing: await location.course,
              country: await regeocode?.get_country(),
              province: await regeocode?.get_province(),
              city: await regeocode?.get_city(),
              cityCode: await regeocode?.get_citycode(),
              adCode: await regeocode?.get_adcode(),
              district: await regeocode?.get_district(),
              poiName: await regeocode?.get_POIName(),
              street: await regeocode?.get_street(),
              streetNumber: await regeocode?.get_number(),
              aoiName: await regeocode?.get_AOIName(),
              accuracy: await location.horizontalAccuracy,
              speed: await location.speed,
            ));
          }
        },
      );
      return completer.future;
    },
  );
}