【问题标题】:Geolocator Plugin to get current locationGeolocator Plugin 获取当前位置
【发布时间】:2019-09-01 19:51:54
【问题描述】:

我正在使用Geolocator插件获取设备的当前位置,以及google 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" />

【问题讨论】:

  • 您确定您在位于android/app/src/main 目录的AndroidManifest.xml 文件中拥有这些权限吗?
  • 是的,我确定,奇怪的是我使用另一台笔记本电脑测试它并且它可以工作,但是在 myPc 中它不工作它要求许可

标签: google-maps dart flutter


【解决方案1】:

将此包添加到flutter项目中.. - pubspec.yaml

-依赖:

  location: ^2.3.4

【讨论】:

  • 它给出了和上面一样的东西
  • android studio 使用 invalidcatch/restart 选项再次重启
【解决方案2】:

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

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

安装这个: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;
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多