google-maps - 用于获取当前位置的地理定位器插件

标签 google-maps dart flutter

我正在使用 Geolocator 插件获取设备的当前位置,并使用 google map 插件将 map 小部件集成到 flutter 中

谷歌地图工作正常,但 Geolocator 给我这个错误:

D/permissions_handler(10148): No permissions found in manifest for: $permission
D/permissions_handler(10148): No permissions found in manifest for: $permission

错误仍然出现,知道为什么会这样吗?

并且在文件Androidmanifest.xml中我添加了这些权限

<manifest>:
  <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" />

最佳答案

问题是 google_maps_flutter 包需要访问您的位置的权限,但该包没有本地代码来请求该权限。

因此您需要编写 native 代码或只安装另一个能够获得该权限的包。

安装这个:https://pub.dartlang.org/packages/location

然后:

getLocationPermission() async {
    final Location location = new Location();
    try {
      location.requestPermission(); //to lunch location permission popup
    } on PlatformException catch (e) {
      if (e.code == 'PERMISSION_DENIED') {
        print('Permission denied');
      }
    }
  }

或者如果你想要更可靠的代码,这是我的一些项目的代码(带有位置包):

//Show some loading indicator depends on this boolean variable
bool askingPermission = false;

@override
  void initState() {
    this.getLocationPermission();
    super.initState();
  }

  Future<bool> getLocationPermission() async {
    setState(() {
      this.askingPermission = true;
    });
    bool result;
    final Location location = Location();
    try {
      if (await location.hasPermission())
        result = true;
      else {
        result = await location.requestPermission();
      }
      print('getLocationPermission: '
          '${result ? 'Access Allowed' : 'Access Denied'}');
    } catch (log, trace) {
      result = false;
      print('getLocationPermission/log: $log');
      print('getLocationPermission/trace: $trace');
    } finally {
      setState(() {
        this.askingPermission = false;
      });
    }
    return result;
  }

关于google-maps - 用于获取当前位置的地理定位器插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55628060/

相关文章:

javascript - 外部脚本加载后运行Vue组件的方法

javascript - 查找设备的位置

dart - 创建页面时未调用异步函数

dart - 是否可以无延迟地使用超时?

list - 访问存储在 map 列表内的键值对中的列表项

ios - 如何使用swift更改谷歌地图中我的位置按钮的位置

google-maps - Google Maps API 未将标记居中

flutter - 如何选择多个图像并通过Rest API Flutter上传

flutter - 如何在Flutter中使用带标签的窗口小部件?

Dart/SQFlite 导入错误