execOnceLocation method

void execOnceLocation({
  1. String clientKey = "onceLocation",
  2. AMapLocationMode locationMode = AMapLocationMode.Hight_Accuracy,
  3. DesiredAccuracy desiredAccuracy = DesiredAccuracy.Best,
  4. required LocationCallback callback,
})

获取一次定位

Implementation

void execOnceLocation({
  String clientKey = "onceLocation",
  AMapLocationMode locationMode = AMapLocationMode.Hight_Accuracy,
  DesiredAccuracy desiredAccuracy = DesiredAccuracy.Best,
  required LocationCallback callback,
}) {
  //已在后台定位中,直接返回定位
  if (currentLocation != null) {
    callback(currentLocation!);
    return;
  }
  if (!clientMap.containsKey(clientKey)) {
    GaodeLocation? location = GaodeLocation();
    //监听定位返回
    location.onLocationChanged().listen((Map<String, Object> result) {
      LocationInfo location = LocationInfo.fromJson(result);
      callback(location);
      destroyLocation(clientKey: clientKey);
    });
    clientMap[clientKey] = location;
  }
  LocationOption option = LocationOption(
    onceLocation: true,
    locationMode: locationMode,
    desiredAccuracy: desiredAccuracy,
  );
  //适配ios14及以上 精准定位权限
  option.fullAccuracyPurposeKey = fullAccuracyPurposeKey ?? "purposeKey";
  clientMap[clientKey]?.setLocationOption(option);
  clientMap[clientKey]?.startLocation();
}