首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Flutter Geolocator.getCurrentPosition永远不会返回

Flutter Geolocator.getCurrentPosition永远不会返回
EN

Stack Overflow用户
提问于 2021-11-18 10:25:48
回答 1查看 306关注 0票数 1

我在Flutter上使用谷歌地图应用程序接口,而Geolocator.getCurrentPosition永远不会返回。这是我的代码(主要取自Simone Alessandria的Flutter项目和一些试图解决这个问题的网络更改)

代码语言:javascript
运行
复制
Future _getCurrentLocation() async {
    bool isGeolocationAvailable = await Geolocator.isLocationServiceEnabled();
    if (isGeolocationAvailable) {
      try {
          Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best, timeLimit: Duration(seconds: 10)).then((pos) {
          setPosition(pos);
        });
      } catch (error) {
        print(error.toString());
        Geolocator.getLastKnownPosition().then((pos) {
          setPosition(pos);
        });
      }
    }
    return null;
  }

正如我提到的,Geolocator.getCurrentPosition永远不会返回,在时间限制下,至少我得到了一个例外。

这是我的扑动医生以防万一:

代码语言:javascript
运行
复制
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.5.1, on Ubuntu 20.04.3 LTS 5.11.0-38-generic, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] Android Studio (version 4.1)
[✓] VS Code
[✓] Connected device (1 available)

• No issues found!

和版本

代码语言:javascript
运行
复制
environment:
  sdk: ">=2.14.2 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  http: ^0.13.3
  google_maps_flutter: ^2.0.10
  permission_handler: ^8.1.6
  geolocator: ^7.7.0

我在Pixel模拟器中运行

谢谢!

EN

Stack Overflow用户

发布于 2021-11-18 10:53:02

这可能与收集位置时使用的距离过滤器有关。这可能会发生,因为当你打开一个应用程序时,它需要currentLocation。然后,当您想要再次获取电流时,滤波器会检测到该位置太近,不会将其传递出去。在我的应用程序中,我首先尝试getLastKnowPosition,然后当它为空时,我从getCurrentPosition获取它。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70018164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档