var geoLocator = Geolocator();
var status = await geoLocator.checkGeolocationPermissionStatus();
if (status == GeolocationStatus.denied)
// Take user to permission settings
else if (status == GeolocationStatus.disabled)
// Take user to location page
else if (status == GeolocationStatus.restricted)
// Restricted
else if (status == GeolocationStatus.unknown)
// Unknown
else if (status == GeolocationStatus.granted)
// Permission granted and location enabled
bool serviceStatus = await _locationService.serviceEnabled();
if (service) {
// service enabled
} else {
// service not enabled, restricted or permission denied
}
final GeolocationResult result = await Geolocation.isLocationOperational();
if(result.isSuccessful) {
// location service is enabled, and location permission is granted
} else {
// location service is not enabled, restricted, or location permission is denied
}
checkLocationPermission() async {
final status = await Permission.location.request();
if (status == PermissionStatus.granted) {
debugPrint('Permission granted');
bool isLocationEnabled = await Geolocator.isLocationServiceEnabled();
if (isLocationEnabled) {
// location service is enabled,
} else {
// open Location settings
await Geolocator.openLocationSettings();
}
} else if (status == PermissionStatus.denied) {
debugPrint(
'Permission denied. Show a dialog and again ask for the permission');
} else if (status == PermissionStatus.permanentlyDenied) {
debugPrint('Take the user to the settings page.');
await openAppSettings();
}
}
6条答案
按热度按时间ycl3bljg1#
更新:(Geolocator 8.0.1)
上一个解决方案:
接受的答案使用过时的插件,您可以使用Geolocator插件,
jogvjijk2#
使用最新版本的Geolocator 5.0
我用这个方法来检查和启用GPS如果禁用。
unhi4e5o3#
更新2019/10/25
location包现在有一个函数(
serviceEnabled()
)来检测位置服务是否被启用,并返回一个bool,如其文档中所述和example中所示:旧答案(包过期)
使用geolocations,您可以检查位置服务是否可操作。它通常包括更多的可定制性,然后位置包。
dxxyhpgq4#
在Geolocator中使用catchError方法,您不必使用位置发布
ohtdti5x5#
@humazed谢谢你的回答,这是对@humazed答案的修改,带有null-safe和Dart 3
发布:Geolocator和AndroidIntent
mi7gmzs66#