无法获得当前位置并没有权限

时间:2019-03-23 09:42:46

标签: flutter flutter-dependencies

我正在使用 地理位置:'^ 3.0.1' Permission_handler:'^ 3.0.0'

现在,我想获取用户的当前位置,并在用户打开地图时将其显示在地图上。

所以我的代码是:

Future<void> requestPermission() async {
    PermissionHandler()
        .checkPermissionStatus(PermissionGroup.location)
        .then((PermissionStatus permissionStatus) async {
      print("Checking Permission " + permissionStatus.toString());
      if (permissionStatus == PermissionStatus.granted) {
        _getCurrentLocation();
      } else {
        print("Asking Permission " + permissionStatus.toString());

        final List<PermissionGroup> permissions = <PermissionGroup>[
          PermissionGroup.locationWhenInUse
        ];
        final Map<PermissionGroup, PermissionStatus> permissionRequestResult =
            await PermissionHandler().requestPermissions(permissions);

        if (PermissionStatus.granted ==
            permissionRequestResult[PermissionGroup.locationWhenInUse]) {
          print("Permission Granted " + permissionStatus.toString());

          _getCurrentLocation();
        }
      }
    });
  }

和权限在android的清单中定义,在IOS的info.list中定义。

现在的问题是,当我运行此函数并调用requestPermission函数时,它会显示弹出窗口,要求获得许可,并且

当我的allow the permission应用因错误而崩溃时:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions: ... java.lang.IllegalStateException: Reply already submitted

,虽然我在应用程序设置中允许了该许可,但许可的结果还是Permission.disabled,并且在许可中它表明该位置是允许的许可。但是我多次尝试打开显示Permission.disabled的应用程序。

,即使我deny,应用也会因相同的错误而崩溃。

所以我得出的结论是:

如果我allowdeny崩溃,是因为它多次请求,即使我允许结果也是Permission.disabled。

视频链接:https://youtu.be/A1DKkw6u4HI

有人可以帮我解决这个问题吗?

Or please tell me how to take the current location map easily

1 个答案:

答案 0 :(得分:1)

  

请告诉我如何轻松获取当前位置图

如果您只需要轻松获取当前位置并且只需要位置许可,则:

您可以将location插件与flutter一起使用:

在您的pubspec.yml中:location : ^2.3.0

然后获取当前位置:

导入位置信息包

 import 'package:location/location.dart' as locationPackage;

将此添加到您的州

 locationPackage.Location _locationService = new locationPackage.Location();
     bool _permission = false;

在initState中或需要当前位置时调用此函数

fetchCurrentLocation() async {
    await _locationService.changeSettings(
        accuracy: locationPackage.LocationAccuracy.HIGH, interval: 1000);

    locationPackage.LocationData location;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      bool serviceStatus = await _locationService.serviceEnabled();
      print("Service status: $serviceStatus");
      if (serviceStatus) {
        _permission = await _locationService.requestPermission();
        print("Permission: $_permission");
        if (_permission) {
          location = await _locationService.getLocation();

          print("Location: ${location.latitude}");
        }
      } else {
        bool serviceStatusResult = await _locationService.requestService();
        print("Service status activated after request: $serviceStatusResult");
        if (serviceStatusResult) {
          fetchCurrentLocation();
        }
      }
    } on PlatformException catch (e) {
      print(e);
      if (e.code == 'PERMISSION_DENIED') {
        //error = e.message;
      } else if (e.code == 'SERVICE_STATUS_ERROR') {
        //error = e.message;
      }
      location = null;
    }
  }
相关问题