4

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)

jtsoc
  • 51
  • 4
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 26 '23 at 05:53
  • Sorry, code added. – jtsoc Jan 26 '23 at 06:22
  • Was the emulator Android or IOS? – Minimumspace Jan 26 '23 at 06:57
  • Thank you. Both emulators, iOS and Android, are working fine, and GPS information is being acquired. The problem seems to be with the actual device. – jtsoc Jan 26 '23 at 07:07
  • Have you called `Geolocator.isLocationServiceEnabled()`, `Geolocator.checkPermission()`, and `Geolocator.requestPermission()`? When you "could not get GPS" does `getGps()` ... future never complete? times out? completes with bogus data? something else? – Chuck Batson Jan 26 '23 at 08:24
  • Permissions are checked with the `permission-handler` package. It can be used in the emulator, so I think there is no problem. I also checked and it seems that the combination of xcode14 and iOS16 is causing this WARNING. I will continue to investigate. [https://stackoverflow.com/questions/73805219/main-thread-warning-with-cllocationmanager-locationservicesenabled](https://stackoverflow.com/questions/73805219/main-thread-warning-with-cllocationmanager-locationservicesenabled) – jtsoc Jan 26 '23 at 11:33
  • we straggle with this issue also , its hard to know if its from permission or the location, please let us know if you find any solution. – abdalmonem Feb 01 '23 at 09:43
  • I was able to solve my problem. The warning was not enough to interfere with the application. 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. Please try it. – jtsoc Feb 02 '23 at 11:15

2 Answers2

1

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.

return await Geolocator.getCurrentPosition(
  desiredAccuracy: LocationAccuracy.high,
  timeLimit: Duration(seconds: 20),
);
jtsoc
  • 51
  • 4
0

Try this

  1. 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.

  2. Import the package in the file

    import 'package:location_permissions/location_permissions.dart';
    
  3. 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.)

    @override
       void initState() {
        ....
       if (Platform.isIOS) {
         location_permission();
       }
       ....
    }
    
  4. Add the following two methods in the same file

      void location_permission() async {
      final PermissionStatus permission = await _getLocationPermission();
      if (permission == PermissionStatus.granted) {
        final position = await geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.best);
    
        // Use the position to do whatever...
      }
     }
    
     Future<PermissionStatus> _getLocationPermission() async {
      final PermissionStatus permission = await LocationPermissions()
          .checkPermissionStatus(level: LocationPermissionLevel.location);
    
      if (permission != PermissionStatus.granted) {
        final PermissionStatus permissionStatus = await LocationPermissions()
            .requestPermissions(
                permissionLevel: LocationPermissionLevel.location);
    
        return permissionStatus;
      } else {
        return permission;
      }
     }
    
Minimumspace
  • 341
  • 2
  • 21
  • Thanks. But it had already been added. – jtsoc Jan 26 '23 at 06:53
  • Updated the answer. Hope checking for permission and asking is the asnwer you are looking for – Minimumspace Jan 26 '23 at 07:05
  • Thank you very much for your thoughtful response.. but It doesn't seem to be a permissions issue. permission is checked using `permission_handler: ^8.2.5`. I can also display the dialog when using the location service and approve it. – jtsoc Jan 26 '23 at 08:27