我在Flutter上使用谷歌地图应用程序接口,而Geolocator.getCurrentPosition永远不会返回。这是我的代码(主要取自Simone Alessandria的Flutter项目和一些试图解决这个问题的网络更改)
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永远不会返回,在时间限制下,至少我得到了一个例外。
这是我的扑动医生以防万一:
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!
和版本
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模拟器中运行
谢谢!
发布于 2021-11-18 10:53:02
这可能与收集位置时使用的距离过滤器有关。这可能会发生,因为当你打开一个应用程序时,它需要currentLocation。然后,当您想要再次获取电流时,滤波器会检测到该位置太近,不会将其传递出去。在我的应用程序中,我首先尝试getLastKnowPosition,然后当它为空时,我从getCurrentPosition获取它。
https://stackoverflow.com/questions/70018164
复制相似问题