Geolocator插件获取当前位置 [英] Geolocator Plugin to get current location

查看:188
本文介绍了Geolocator插件获取当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Geolocator plugin to get the current location of the device, and google map plugin to integrate the map widget in the flutter

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

The google map works fine, but the Geolocator it gives me this error:

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

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

and the error still appears, any idea of why this happening?

在文件 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软件包需要访问您位置的权限但是该程序包没有本机代码来请求该权限。

The problem is google_maps_flutter package needs permission for accessing your location but the package has not native codes to ask that permission.

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

So you need to write native code or just install another package which is able to take that permission.

安装此文件: 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');
      }
    }
  }

或者如果您想更扎实代码,这是我的某些项目的代码(带有位置包):

Or if you want more solid code, this is my code for some project(with location package):

//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;
  }

这篇关于Geolocator插件获取当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆