获取当前位置的Geolocator插件

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

我正在使用Geolocator插件来获取设备的当前位置,并使用谷歌地图插件将地图小部件集成到颤动中

谷歌地图工作正常,但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 dart flutter
2个回答
1
投票

问题是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;
  }

0
投票

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

-dependencies:

  location: ^2.3.4
© www.soinside.com 2019 - 2024. All rights reserved.