Reputation: 19
I'm working with Google maps API on Flutter and Geolocator.getCurrentPosition never returns. This is my code (mostly taken from Flutter Projects by Simone Alessandria and some changes taken from the web trying to fix this issue)
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;
}
As I mentioned Geolocator.getCurrentPosition never returns, with the time limit at least I get an exeption.
Here's my Flutter doctor just in case:
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!
And the versions
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
I'm running in the Pixel emulator
Thanks!
Upvotes: 2
Views: 2840
Reputation: 4130
If you are running on an IOS simulator
,
Go to Features > Location > Set to custom and set it back to default
Upvotes: 0
Reputation: 69
It may be related to the distance filter you have when collecting locations. This can happen because when you open an app it takes the currentLocation. Then when you want to fetch current again the filter detects that the location is too close and does not pass it on. In my app I try to getLastKnowPosition first, then when it is null I get it from getCurrentPosition.
Upvotes: 5