dart GPS是否激活-Flutter

pinkon5k  于 2023-06-19  发布在  Flutter
关注(0)|答案(6)|浏览(107)

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

ycl3bljg

ycl3bljg1#

更新:(Geolocator 8.0.1)
bool isLocationEnabled = await Geolocator.isLocationServiceEnabled();

上一个解决方案:

接受的答案使用过时的插件,您可以使用Geolocator插件,

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
jogvjijk

jogvjijk2#

使用最新版本的Geolocator 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();
                  },
                ),
              ],
            );
          },
        );
      }
    }
  }
unhi4e5o

unhi4e5o3#

更新2019/10/25

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

bool serviceStatus = await _locationService.serviceEnabled();
if (service) {
    // service enabled
} else {
    // service not enabled, restricted or permission denied
}

旧答案(包过期)

使用geolocations,您可以检查位置服务是否可操作。它通常包括更多的可定制性,然后位置包。

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 
}
dxxyhpgq

dxxyhpgq4#

在Geolocator中使用catchError方法,您不必使用位置发布

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

ohtdti5x5#

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

发布:GeolocatorAndroidIntent

// 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();
                  },
                ),
              ],
            );
          },
        );
      }
    }
  }
mi7gmzs6

mi7gmzs66#

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();
}
}

相关问题