Is GPS activated - Flutter

前端 未结 3 914
执念已碎
执念已碎 2020-12-28 10:38

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.

3条回答
  •  失恋的感觉
    2020-12-28 11:08

    The previous answers are outdated.

    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: [
                    FlatButton(
                      child: Text('Ok'),
                      onPressed: () {
                        final AndroidIntent intent = AndroidIntent(
                            action: 'android.settings.LOCATION_SOURCE_SETTINGS');
    
                        intent.launch();
                        Navigator.of(context, rootNavigator: true).pop();
                      },
                    ),
                  ],
                );
              },
            );
          }
        }
      }
    

提交回复
热议问题