地理定位器包在 Flutter 中不起作用

问题描述 投票:0回答:1

你们中有人使用过地理定位器软件包吗? 如果是,那么您可以帮忙吗? 我已经完成了 geolocator 包文档中提到的所有步骤,不包括 androidx 转换 我认为我收到的错误是应用程序无法获取位置。未授予许可我不知道为什么 enter image description here

enter image description here

我目前正在模拟器上测试它,而不是真实的设备。

我已经尝试对 ios 和 android 的 android 清单和 info.plist 进行更改,但我不知道为什么它不起作用。有一次,当我运行该应用程序时,它说诸如 API 已过时之类的信息,但在我重建整个应用程序后,该错误并未发生。 如果您知道出了什么问题并且需要有关我的代码的一些其他信息。请随时发表评论,我将为您提供必要的详细信息。

flutter geolocation flutter-dependencies geolocator
1个回答
0
投票

**

你尝试过这个吗?

**

 serviceEnabled = await Geolocator.isLocationServiceEnabled();

    if (serviceEnabled) {
      serviceEnabled = true;
    } else {
      serviceEnabled = false;
    }

    LocationPermission _permission = await Geolocator.checkPermission();

    if (_permission == LocationPermission.denied) {
      _permission = await Geolocator.requestPermission();
      if (_permission == LocationPermission.always || _permission == LocationPermission.whileInUse) {
        permissionGranted = true;
      } else {
        permissionGranted = false;
      }
    } else {
      if (_permission == LocationPermission.always || _permission == LocationPermission.whileInUse) {
        permissionGranted = true;
      } else {
        permissionGranted = false;
      }
    }

    if (serviceEnabled && permissionGranted) {
      try {
        locationData = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.bestForNavigation, timeLimit: const Duration(seconds: 4));
      } catch (e) {}
    }
© www.soinside.com 2019 - 2024. All rights reserved.