如标题所述,有什么方法可以连续检查geoLocator.isLocationServiceEnabled()吗?

我想一直检查GPS状态,并在用户打开/关闭GPS的情况下显示图像。


void _getLocation()  {
    Geolocator geoLocator = Geolocator();
    try {

      geoLocator.checkGeolocationPermissionStatus().then((granted) async {
        if (granted != null) {

          noGPS = !await geoLocator.isLocationServiceEnabled();

          bool firstRun = true;
          geoLocator.getPositionStream(LocationOptions(
              distanceFilter: 20,
              accuracy: LocationAccuracy.best,
              timeInterval: 10000),GeolocationPermission.locationWhenInUse)
              .listen((position) {
                  print(position.longitude);
                  longitude = position.longitude;
                  latitude= position.latitude;
                  if(firstRun){
                    mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
                      target: LatLng(latitude, longitude),
                      zoom: 15,
                    )));
                  }
                  firstRun=false;
                  _addGeoPoint();
          }
          );

        }else {
          noGPS=true;
        }
      });
    } on Exception {

    }
  }

最佳答案

我最终以这种方式这样做

                  FutureBuilder<bool>(
                  future: geoLocator.isLocationServiceEnabled(),
                  initialData: false,
                  builder:
                      (BuildContext context, AsyncSnapshot<bool> snapshot) {
                    if (!snapshot.data) {
                      return FadeTransition(
                          opacity: _animationController,
                          child: IconButton(
                            icon: Icon(Icons.gps_fixed),
                            onPressed: () => null,
                            color: Colors.red,
                          ));

关于android - 无论如何,是否要连续检查geoLocator.isLocationServiceEnabled()?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58887368/

10-11 01:15