【问题标题】:Is there anyway to continuously check geoLocator.isLocationServiceEnabled()?无论如何要不断检查 geoLocator.isLocationServiceEnabled() 吗?
【发布时间】:2019-11-16 03:43:36
【问题描述】:

如标题所说,有没有办法不断检查geoLocator.isLocationServiceEnabled()?

我想随时检查 GPS 状态,并在 GPS 被用户打开/关闭时显示图像。

需要持续运行 await 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 {

    }
  }

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    我最终是这样做的

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

    【讨论】:

      猜你喜欢
      • 2012-10-21
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      相关资源
      最近更新 更多