0

嗨,我正在尝试通过在颤振中使用位置包和 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" />

尽管我已经添加了该代码,但我仍然收到错误。我应该做些什么来防止这个问题?

4

1 回答 1

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();
于 2020-05-13T02:02:10.237 回答