Is GPS activated - Flutter

2019-02-19 22:47发布

问题:

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.

回答1:

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 
}


回答2:

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


标签: dart flutter