在权限不起作用的情况下获取当前位置

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

我正在使用geolocator:'^ 3.0.1'permission_handler:'^ 3.0.0'

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

所以我的代码是:

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

flutter flutter-dependencies
1个回答
1
投票

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

如果您只需要轻松获取当前位置,而您只需要位置权限,那么:

你可以使用location插件与颤动:

在你的pubspec.ymllocation : ^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;
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.