I am developing in Flutter and am trying to get GPS information using the geolocator package.
I was able to get GPS information on the emulator with no problem, but when I started the app on the actual device, I could not get GPS.
The app was working fine, so it was not an error, but there was a message.
"This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the -locationMana gerDidChangeAuthorization: callback and checking authorizationStatus first."
Code on Flutter side
Future<Position> getGps(BuildContext context) async {
return await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high,
timeLimit: Duration(seconds: 5),
);
}
(WARNING)Code on IOS side
- (ServiceStatus)checkServiceStatus:(PermissionGroup)permission {
return [CLLocationManager locationServicesEnabled] ? ServiceSttus Enabled
: ServiceStatusDisabled;
}
2023/1/26 added
I had added it to info.plist. Still, I am not getting GPS information.
info.plist
<key>NSLocationAlwaysUsageDescription</key>
<string>Access location information to get a location</string>.
<key>NSLocationWhenInUseUsageDescription</key>
<string>Access location information to get location</string>
How do I get GPS information?
Permission to acquire location services is granted.
iOS:16.2
xcode: 14
geolocator: 7.7.1
2023/1/26 added
Current Status
Emulator(Android) …〇(GPS information has been acquired.)
Emulator(iOS(iphone SE2)) …〇(GPS information has been acquired.)
Actual Device(iOS(iphone SE2)) …× (GPS cannot be acquired)
2
Answers
I was able to solve my problem. I thought it was a permissions issue in my case, but it was just a location acquisition timeout. I modified the location acquisition timeout from 5 seconds to 20 seconds and was able to get the necessary information without any problems.
The warnings are still there, but we decided not to worry about them since we were able to solve the problem.
Try this
Add "location_permissions: 3.0.0+1" this dependencies in "pubspec.yaml". Please note that I did that for flutter 1.22.0 so for flutter 2.0 this might be an issue.
Import the package in the file
Add the following code on the page where you want to ask for permission. (Better to add that on the very first page of your app.)
Add the following two methods in the same file