【问题标题】:How to handle "gps" location provider requires ACCESS_FINE_LOCATION permission. in flutter apps如何处理“gps”位置提供者需要 ACCESS_FINE_LOCATION 权限。在颤振应用中
【发布时间】:2020-08-29 00:59:57
【问题描述】:

您好,我正在尝试通过在 Flutter 中使用位置包和 google-maps-flutter 来获取用户位置。但我收到一条错误消息,提示 "gps" location provider requires ACCESS_FINE_LOCATION permission. 我已在我的 androidmanifest 文件中添加了此代码

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

虽然我添加了该代码,但我仍然收到错误消息。我应该做些什么来防止这个问题发生?

【问题讨论】:

    标签: flutter location google-maps-flutter


    【解决方案1】:

    您应该检查您的自我许可状态,如果它没有被用户授予,您应该请求许可。您可以使用位置插件来请求权限。

    https://pub.dev/packages/location

    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();
    

    【讨论】:

    • 嗨..我尝试了你的代码,但我在控制台中收到了这条消息There is no constant named 'granted' in 'PermissionStatus'. Try correcting the name to the name of an existing constant, or defining a constant named 'granted'.
    • 有什么需要补充的吗?
    • 我将deniedgranted 更改为大写锁定,它可以工作.. 谢谢你的帮助
    猜你喜欢
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 2016-02-25
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    相关资源
    最近更新 更多