在Android平台上我执行Geolocator()的时候。 CheckGeolocationPermissionStatus() 函数第一次拒绝GeolocationStatus 并且不请求用户许可。 但是在 IOS 中,当我第一次调用相同的函数时,GeolocationStatus 是未知的,因此我知道我必须调用 Geolocator()。 GetCurrentPosition() 向用户询问权限并在授予权限时返回位置。
当我第一次调用 checkGeolocationPermissionStatus 时,有什么方法可以在 Android 中请求用户许可吗?
在我的代码下面:
Future<void> getLocation() async {
try {
var serviceEnabled = await Geolocator().isLocationServiceEnabled();
if (serviceEnabled) {
var geolocationStatus =
await Geolocator().checkGeolocationPermissionStatus();
if (geolocationStatus == GeolocationStatus.granted || geolocationStatus == GeolocationStatus.unknown) {
position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
return;
}
}
} catch (e) {
print(e);
}
}
似乎这个插件管理器对维护或支持其社区并不感兴趣,我看到了很多问题,但没有针对它们的准确或错误修复解决方案。
是啊,你说得对! 我看到其他问题也没有答案。 =/
@azeemgujjar , @CNogueira92 ,情况并非如此。 我实际上正在努力开发下一个主要版本,但作为大多数 OSS 项目,我需要在我的业余时间这样做,目前有点有限,所以我的注意力有点分散。 当然,我欢迎对插件的贡献以提供帮助。
要回答您的问题@CNogueira92 ,Android 和 iOS 处理权限的方式略有不同。 默认情况下,Android 将权限视为“拒绝”(即使用户没有明确拒绝)。 对于尚未请求权限的情况,iOS 具有特定状态(称为“未确定”,我们将其翻译为“未知”)。
在这两个平台上,当checkGeolocationPermissionStatus
返回denied
或unknown
时请求权限是可以的。 如果用户选择了“拒绝且不再询问”选项,地理定位器将注意到这一点,而不是请求权限。
实际上,您甚至不必手动检查权限,因为 Geolocator 会在内部进行检查。 因此,您可以安全地将代码更新为:
Future<void> getLocation() async {
try {
var serviceEnabled = await Geolocator().isLocationServiceEnabled();
if (serviceEnabled) {
position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
} else {
// TODO: ask the user to enable the location services...
}
} catch (e) {
print(e);
}
}
checkGeolocationPermissionStatus
只是一种方便的方法,以防您想手动检查权限并通知用户采取某些行动。 在我提到的新版本中,您还可以手动请求权限。 如果您现在想这样做,您需要调用地理定位器附带的location_permissions
插件。 可以在此处找到有关 location_permissions 插件的更多信息。