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;
  }
  clientMap.putIfAbsent(clientKey, () => AMapFlutterLocation());
  AMapFlutterLocation? location = clientMap[clientKey];

  //没有后台定位,这里执行一次就可以了
  location?.onLocationChanged().listen((Map<String, Object> result) {
    LocationInfo location = LocationInfo.fromJson(result);
    callback(location);
    destroyLocation(clientKey: clientKey);
  });
  AMapLocationOption option = AMapLocationOption(
    onceLocation: true,
    locationMode: locationMode,
    desiredAccuracy: desiredAccuracy,
  );
  //适配ios14及以上 精准定位权限
  option.fullAccuracyPurposeKey = fullAccuracyPurposeKey ?? "purposeKey";
  location?.setLocationOption(option);
  location?.startLocation();
}