Flutter-geolocator: GPS current Location is not showing but last known location is showing

Created on 28 Sep 2020  Â·  8Comments  Â·  Source: Baseflow/flutter-geolocator

💬 Questions and Help

Hello I'm using current version of the Geolocator , it's not working as it should , I'm getting last known location on device, permission is given. Testing on physical device. Any suggestions would be great. Thank you!

android

Most helpful comment

@ujjwalbe, @Moustrash, which permissions did you have in your AndroidManifest.xml file? This behavior occurs when you have ACCESS_COARSE_LOCATION in your AndroidManifest.xml and should be solved by changing to the ACCESS_FINE_LOCATION permission.

This has also recently been updated in the README.md file which now explains:

NOTE: Specifying the ACCESS_COARSE_LOCATION permission results in only being allowed to request a location with the getLastKnownPosition method. The accuracy can be very precise or could be the equivalent to a city block. If the phone already knows where he is this results in a quick response, but it could also take a long time (minutes) before you will get your first locations fix.

What this says is:

If you are only using the getLastKnownPosition in your code the ACCESS_COARSE_LOCATION permission is sufficient. If you are using the getCurrentPosition or getPositionStream methods in your code you must use the ACCESS_FINE_LOCATION permission to receive the actual location information.

All 8 comments

@ujjwalbe could you provide more information about your setup? I would be interested in the following:

  1. On which platform(s) do you experience the issue (Android, iOS or both)?
  2. Can you post an example of the code you are using to acquired the location?
  3. Can you attach the output of the flutter doctor -v command?

Thank you for responding.
The details you asked for are below:

  1. Android
  2. Code:
 class GeolocatorService {
   Future<Position> getLocation() async {
     Position pos;
     try {
        pos = await getCurrentPosition(
           desiredAccuracy: LocationAccuracy.best,
           timeLimit: Duration(seconds: 10));
     } catch (err) {
       pos = await getLastKnownPosition();
     }
     print("hello");
     print("${pos.latitude} ${pos.longitude}");
     return pos;
   }

   Future<Position> getLastLocation() async {
     print("heellos");
     Position position = await getLastKnownPosition();
     print("${position.longitude} ${position.latitude}");
     return position;
   }


 }
  1. Output of flutter doctor -v command:
Flutter (Channel stable, 1.20.4, on Linux, locale en_IN)
    • Flutter version 1.20.4 at /home/ujjwal/Android/flutter
    • Framework revision fba99f6cf9 (2 weeks ago), 2020-09-14 15:32:52 -0700
    • Engine revision d1bc06f032
    • Dart version 2.9.2


[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /home/ujjwal/Android/Sdk
    • Platform android-30, build-tools 30.0.1
    • Java binary at: /home/ujjwal/Applications/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Android Studio (version 4.0)
    • Android Studio at /home/ujjwal/Applications/android-studio
    • Flutter plugin version 48.0.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.49.1)
    • VS Code at /usr/share/code
    • Flutter extension version 3.14.1

[✓] Connected device (1 available)
    • Redmi Note 5 Pro (mobile) • cfb3aab9 • android-arm64 • Android 9 (API 28)

• No issues found!

Hi @ujjwalbe, thank you for providing the requested feedback. But I am not sure I understand your question completely?

  • Do you have a problem that you receive the last known position as output from the getCurrentPosition method?
  • Or do you have a problem with the getCurrentPosition method timing out and you code runs the catch block?

I added trycatch after not getting any result from getCurrentLocation and added timeout, i had no issue with getLastKnownPosition.
I don't know why it was happening then i used location library which worked fine, without any issues. I previously used same method using geolocator had no issues but don't know why it's giving any result now.

Hello, I'm having the same issue as @ujjwalbe, using getCurrentLocation() has a weird behavior, neither do I enter the following .then() nor the .catchError() unless I specify a time limit which is eventually reached and triggers the .catchError().
Debugging gives no information on why this method fails, I thus use getLastKnownLocation() to have geolocation until more information is available about this bug.
I'm using geolocator v6.0.0+4, geolocator_platform_interface v1.0.4, geocoding v1.0.4+1 and geocoding_platform_interface v1.0.1+1
I'm experiencing the issue on Android as I can't test on iOS rigth now, and here is the code that fails:

_getCurrentLocation() async {
    setState(() {
      _loading = true;
    });
    Permission.location.request().then((value) {
      getCurrentPosition(
              timeLimit: const Duration(seconds: 3),
              forceAndroidLocationManager: true)
          .then((Position position) => _getAddressFromLatLng(position))
          .catchError((e) {
        setState(() {
          _loading = false;
        });
      });
    });
  }

Output of flutter doctor -v is :

[√] Flutter (Channel stable, 1.20.3, on Microsoft Windows [version 10.0.18363.1082], locale fr-FR)
    • Flutter version 1.20.3 at C:\Users\***\Documents\flutter_windows_v1.9.1+hotfix.6-stable\flutter
    • Framework revision 216dee60c0 (5 weeks ago), 2020-09-01 12:24:47 -0700
    • Engine revision d1bc06f032
    • Dart version 2.9.2








[!] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Users\***\AppData\Local\Android\sdk
    • Platform android-30, build-tools 29.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
    X Android license status unknown.
      Try re-installing or updating your Android SDK Manager.
      See https://developer.android.com/studio/#downloads or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed
      instructions.



[√] Android Studio (version 3.6)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 47.1.1
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)



[√] Connected device (1 available)
    • VOG L29 (mobile) • 22X0219C19015371 • android-arm64 • Android 10 (API 29)



! Doctor found issues in 3 categories.

I also had similar issue that's why i used timeout, but it did not worked. For me location library worked fine @Moustrash

@ujjwalbe, @Moustrash, which permissions did you have in your AndroidManifest.xml file? This behavior occurs when you have ACCESS_COARSE_LOCATION in your AndroidManifest.xml and should be solved by changing to the ACCESS_FINE_LOCATION permission.

This has also recently been updated in the README.md file which now explains:

NOTE: Specifying the ACCESS_COARSE_LOCATION permission results in only being allowed to request a location with the getLastKnownPosition method. The accuracy can be very precise or could be the equivalent to a city block. If the phone already knows where he is this results in a quick response, but it could also take a long time (minutes) before you will get your first locations fix.

What this says is:

If you are only using the getLastKnownPosition in your code the ACCESS_COARSE_LOCATION permission is sufficient. If you are using the getCurrentPosition or getPositionStream methods in your code you must use the ACCESS_FINE_LOCATION permission to receive the actual location information.

Closing this issue, seems to be solved by adding the correct permissions

Was this page helpful?
0 / 5 - 0 ratings