无论如何,是否要连续检查geoLocator.isLocationServiceEnabled()?

时间:2019-11-16 03:43:36

标签: android flutter dart

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

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

  

需要连续运行以等待geoLocator.isLocationServiceEnabled();

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 {

    }
  }

1 个答案:

答案 0 :(得分:0)

我最终还是这样

                  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,
                          ));