是否激活了 GPS - 颤振



有没有办法在 Flutter 中找出 GPS 是被激活还是停用? 我使用插件位置,但是在那里我只得到位置,而不是GPS的状态。

更新:(地理定位器 8.0.1(

bool isLocationEnabled = await Geolocator.isLocationServiceEnabled();
<小时 />

以前的解决方案:

接受的答案使用过时的插件,您可以使用地理定位器插件,

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

使用最新版本的地理定位器 5.0

var isGpsEnabled = await Geolocator().isLocationServiceEnabled();

如果禁用,我使用此方法检查并启用 GPS。

Future _checkGps() async {
if (!(await Geolocator().isLocationServiceEnabled())) {
if (Theme.of(context).platform == TargetPlatform.android) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Can't get gurrent location"),
content:
const Text('Please make sure you enable GPS and try again'),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () {
final AndroidIntent intent = AndroidIntent(
action: 'android.settings.LOCATION_SOURCE_SETTINGS');
intent.launch();
Navigator.of(context, rootNavigator: true).pop();
},
),
],
);
},
);
}
}
}

更新 2019/10/25

位置包现在有一个函数 (serviceEnabled()( 来检测位置服务是否已启用并返回一个 bool,如其文档中所述,如示例中所示:

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 
}

将 catchError 方法与地理定位器一起使用,您不必使用位置发布

await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
// show location
});
}).catchError((e) {
return Future.error('Location services are disabled.');
});

@humazed感谢您的回答,这是对 @humazed 的答案的修改,带有 null-safety 和 Dart 3

发布:Geolocator 和 AndroidIntent

// Check the GPS is on
Future _checkGps() async {
if (!(await Geolocator.isLocationServiceEnabled())) {
if (!mounted) return;
if (Theme.of(context).platform == TargetPlatform.android) {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("Can't get current location"),
content:
const Text('Your GPS is turn off, please turn it on first.'),
actions: <Widget>[
TextButton(
child: const Text('Turn On'),
onPressed: () async {
const AndroidIntent intent = AndroidIntent(
action: 'android.settings.LOCATION_SOURCE_SETTINGS');
await intent.launch();
if (!mounted) return;
Navigator.of(context, rootNavigator: true).pop();
},
),
],
);
},
);
}
}
}
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();
}
}

最新更新