Flutter:检查GPS定位模式

问题描述 投票:0回答:1

我有使用地图和跟踪位置的用户相关的项目,为此我使用了Geolocator Package。我需要检查用户的当前位置,首先在我的流程项目中,检查用户的最后位置,如果最后位置为null,则检查用户的当前位置。

代码

  Position _currentPosition;
  Position get currentPosition => _currentPosition;

  Future<void> getCurrentPosition() async {
    try {
      Position lastPosition = await Geolocator().getLastKnownPosition();
      if (lastPosition != null) {
        print("Success Get Last Position...");
        _currentPosition = lastPosition;
      } else {
        final currentPosition = await Geolocator().getCurrentPosition();
        if (currentPosition != null) {
          print("Success Get Your Current Position...");
          _currentPosition = currentPosition;
        } else {
          throw "Can't Get Your Position";
        }
      }
    } catch (e) {
      throw e;
    }
    notifyListeners();
  }

然后我在按钮OnPressed上调用getCurrentPosition函数来获取这样的位置用户:

Button GetLocation

void goToMaps() async {
    final mapsProvider = context.read<MapsProvider>();
    try {

      print('Get Location User');
      await mapsProvider.getCurrentPosition();
      print('Success Get location User');

    } catch (e) {
      globalF.showToast(message: e.toString(), isError: true, isLongDuration: true);
    }
  }

第一个问题是,当GPS定位模式为Device Only时,我无法获取LastPosition当前位置,它从不打印成功获取位置用户,而仍然打印获取位置用户

第二个问题是,当Gps定位模式为Battery Saving时,与使用模式High Accuracy相比,我得到的用户位置非常不准确。

我的问题是,如何检查gps位置模式!= 高精度,然后我可以显示警告用户,将gps设置为高精度。

像这样的question

谢谢。

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS83SDVHei5qcGcifQ==” height =“ 500”>

flutter geolocation gps
1个回答
0
投票

对于第二个问题,它的发生是因为在高精度模式下android同时使用GPS和网络位置,但是在省电模式下GPS模块已关闭。如果是本地Android,则可以从LocationManager获取位置,然后检查位置的provider。但是在Flutter中Position类中没有提供程序。您可以改用accuracy。如果它大于阈值并且不适合您的应用程序,则可以建议用户打开“高精度”模式。

© www.soinside.com 2019 - 2024. All rights reserved.