android - 无论如何要不断检查 geoLocator.isLocationServiceEnabled()?

标签 android flutter dart

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

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

need to continuously run 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 {

    }
  }

最佳答案

我是这样做的

                  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/

相关文章:

android - ImageView 填充 parent 的宽度或高度,但保持纵横比

flutter - 在应用运行时从URL读取数据

ios - iOS 上的 flutter : How to trigger pop-up for allowing local network access

Flutter - 当键盘打开时它会阻塞我的文本字段

android - 位置参数必须出现在命名参数之前,并且 flutter 上的位置参数太多

java - 不一致的空指针异常

android - 使用 AccountPicker.newChooseAccountIntent 选择电子邮件

c# - 应用程序级别变量

widget - Flutter Inspector Toggle Select Widget 模式不起作用。

dart - dart计时器定期文档令人困惑