【发布时间】:2021-12-29 05:50:44
【问题描述】:
我正在 Flutter 上使用 Google 地图 API,而 Geolocator.getCurrentPosition 永远不会返回。 这是我的代码(主要取自 Simone Alessandria 的 Flutter Projects,并从网络上进行了一些更改以试图解决此问题)
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 永远不会返回,至少我得到一个例外的时间限制。
这是我的 Flutter 医生以防万一:
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
我在像素模拟器中运行
谢谢!
【问题讨论】:
标签: android flutter google-maps future