带定位包的 FLutter 高精度

FLutter high accuracy with location package

为了获取用户的位置,我使用了两个包:locationgeocoding。我使用位置来检查用户是否提供了位置权限,并获得带有经纬度的LocationData

但据我所知,location 没有提供名为 desiredAccuracy 的 属性,这 非常适合我的工作。

  static void getCurrentLocation() async {
    Location location = new Location();
    bool _serviceEnabled;
    PermissionStatus _permissionGranted;
    LocationData _locationData;

    _serviceEnabled = await location.serviceEnabled();
    if (!_serviceEnabled) {
      _serviceEnabled = await location.requestService();
      if (!_serviceEnabled) {
        return;
      }
    }

    _permissionGranted = await location.hasPermission();
    if (_permissionGranted == PermissionStatus.denied) {
      _permissionGranted = await location.requestPermission();
      if (_permissionGranted != PermissionStatus.granted) {
        return;
      }
    }

    _locationData = await location.getLocation();
    double latitude = _locationData.latitude!;
    double longitude = _locationData.longitude!;
    List<geocoding.Placemark> placeMark =
        await geocoding.placemarkFromCoordinates(latitude, longitude);
    String country = placeMark.first.country!;
    String isoCountryCode = placeMark.first.isoCountryCode!;
    print("Country: $country and ISO Country Code: $isoCountryCode");

    print(_locationData);
  }

到目前为止我的代码。是否有可能在 Location 中获得类似 desiredAccuracy 的信息?对我来说唯一的其他解决方案是使用 geolocatorpermission_handler 包,这需要进一步配置。

我不明白你为什么不想使用地理定位器...你已经完成了艰苦的工作。 以下是您如何获得所需准确度的位置

 location = await Geolocator.getCurrentPosition(
  forceAndroidLocationManager: true,
  desiredAccuracy: LocationAccuracy.best
).timeout(Duration(seconds: 20));

检查服务是否启用的方法如下

var serviceEnabled = await Geolocator.isLocationServiceEnabled();

下面是检查权限是否被授予的方法

    var permission = await Geolocator.checkPermission();

    if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) {
      // Permissions are denied, next time you could try
      // requesting permissions again (this is also where
      // Android's shouldShowRequestPermissionRationale
      // returned true. According to Android guidelines
      // your App should show an explanatory UI now.

这里的文档里写的很清楚 geolocatordoc