11

Is there a way to find out in Flutter if the GPS is activated or deactivated? I use the plugin location however there I get only the location and not the state of the GPS.

CopsOnRoad
  • 175,842
  • 51
  • 533
  • 380
GreenTigerEye
  • 4,761
  • 8
  • 21
  • 33

4 Answers4

22

Update: (Geolocator 8.0.1)

bool isLocationEnabled = await Geolocator.isLocationServiceEnabled();

Previous solution:

Accepted answer uses outdated plugin, you can use Geolocator plugin,

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
CopsOnRoad
  • 175,842
  • 51
  • 533
  • 380
18

Using the latest version of Geolocator 5.0

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

I use this method to check and enable the Gps if disabled.

  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();
                  },
                ),
              ],
            );
          },
        );
      }
    }
  }
CopsOnRoad
  • 175,842
  • 51
  • 533
  • 380
humazed
  • 70,785
  • 32
  • 93
  • 133
12

Update 2019/10/25

The location package now has a function (serviceEnabled()) to detect whether the location service is enabled and returns a bool as described in its docs and shown in the example:

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

Old answer (package outdated)

With geolocations you can check wether the location service is operational. It generally includes more customizability then the location package.

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 
}
Bostrot
  • 5,110
  • 3
  • 31
  • 41
-1
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();
}
}
Example person
  • 2,832
  • 3
  • 12
  • 39