Flutter-geolocator: Geolocator.getCurrentPosition() method takes too much time to return its position

Created on 22 Oct 2020  路  17Comments  路  Source: Baseflow/flutter-geolocator

馃悰 Bug Report

The method Geolocator.getCurrentPosition() takes too much time to get the position (almost more than 2 minutes sometimes even more)

Reproduction steps

 Future<Position> getCurrentLocation(
      {LocationAccuracy accuracy =
          LocationAccuracy.high}) async {
      final position = await Geolocator.getCurrentPosition(
          desiredAccuracy: accuracy,
      timeLimit: Duration(seconds: 5));
      if (position != null) _init();
      return position;
  }

Device Configuration - Moto G5 S Plus Android 8.1 Oreo

Version: 6.1.1 of Geolocator

Platform:

  • [ ] :iphone: iOS
  • [*] :robot: Android
android triage

All 17 comments

@mabels @AlexandreRoba @sestegra @ened

Did you enable assisted location services in the system settings of the phone? Depending on that setting and perhaps the GPS coverage in your area, location retrieval can take a while.

Hello. Similar happens with version 6.1.0 both on Samsung S10+ (Android 10) and iPhone 8 (iOS 13.7). Thanks

Did you enable assisted location services in the system settings of the phone? Depending on that setting and perhaps the GPS coverage in your area, location retrieval can take a while.

yes, I have good Internet connection and enabled location services in settings

Hi @balajiramadoss,

This is a bit hard to debug, I have gone over the implementation on my side a few times and I feel this is a problem related to the hardware. Also I don't experience this problem in my tests (on emulator or on the one LG Nexus 5x I own).

As @ened mentioned it can take a while for your phone to acquire a GPS link and retrieve it's current location. Could you check if once the device has acquired a GPS fix, subsequent calls to the getCurrentPosition method return fast? If this is the case it would be a sign indicating the problem is with the hardware and not inside the plugin.

Some workarounds that might help are:

  1. Start acquiring a location as early as possible in your App to make sure the device is connected with GPS network when you really need the position. I realize this will not work for everybody and it will have it's challenges with permission management, but it might do the trick for you;
  2. Call the getLastKnownPosition which will return the last known location that is stored in the device's cache and in the meantime wait for the actual location to come in. Note that in some cases the last known location is not available and the getLastKnownPosition method will return null.

I am open for any other suggestions and feel free to go through the code and give (constructive) feedback.

I wanted to add that I am facing a similar situation. I am passing in a 10 second timeout, and it progresses well beyond 10 seconds and never returns (or throws an exception). I was previously using version 5.1.1+1 without issue, but I upgraded to the most recent version to be able to pass in the timeout value. After upgrading, I did 'flutter clean' before building, just in case.

Some other information that may be relevant: when my application starts, I am setting up a position stream via Geolocator.getPositionStream with the relevant parameters for my situation. The stream is used for location recording, but I have a diagnostics type screen where I call Geolocator.getCurrentPosition to display the most accurate and up-to-date position along with other hardware information.

In version 5.1.1+1, I had no problems having a stream active while calling getCurrentPosition. I updated my diagnostics screen to call getLastKnownPosition instead, and that returned immediately.

I'd really rather have a confirmed current position, but since it is not presently working, I'm falling back to the last known position. The original poster did not indicate whether a stream was already active or not, but it might add some additional insights to the issue.

@zarfleen version 5 of the geolocator had a bug which might explain why it (seemed) to be fast. The problem was that when calling the getCurrentPosition method (and using .forceAndroidLocationManager option) the geolocator always returned the last known location (meaning the cached version and not the actual location).

Strangely enough nobody noticed it and I realized the mistake when rebuilding the plugin into version 6. Maybe you can confirm you were using the .forceAndroidLocationManager?

Currently we do have an issue where the getCurrentPosition method will use the same stream to acquire the location as the getPositionStream while the later is active (see also #546). I will be fixing this a.s.a.p.

@mvanbeusekom I was not using forceAndroidLocationManager. My current issues with getCurrentPosition may be due to the issue you referenced. Once that has been resolved, I will give it a try again and see if I continue having issues with it never returning.

One thing of note: I was recently working on a different project where I was using the FusedLocationProviderClient and noticed that a getCurrentLocation function was recently added. I believe it is only available in version >= 17.1.0 of the play services location library, while this plugin library was using 17.0.0 when I last browsed the source. Updating to use the newer version where that function is available may be an option to resolve the issues with a location stream being open while calling getCurrentPosition.

 Future<Position> getCurrentLocation(
      {LocationAccuracy accuracy =
          LocationAccuracy.high}) async {
      final position = await Geolocator.getCurrentPosition(
          desiredAccuracy: accuracy,
      timeLimit: Duration(seconds: 5));
      return position;
  }

Also this method never returns the Exception when it crosses the timeLimit

@mvanbeusekom I was not using forceAndroidLocationManager. My current issues with getCurrentPosition may be due to the issue you referenced. Once that has been resolved, I will give it a try again and see if I continue having issues with it never returning.

One thing of note: I was recently working on a different project where I was using the FusedLocationProviderClient and noticed that a getCurrentLocation function was recently added. I believe it is only available in version >= 17.1.0 of the play services location library, while this plugin library was using 17.0.0 when I last browsed the source. Updating to use the newer version where that function is available may be an option to resolve the issues with a location stream being open while calling getCurrentPosition.

@zarfleen I have just released version 6.1.2 of the geolocator which contains a fix for issue #546, I would really appreciate it if you could run some tests and see if this also solves your issues?

@mvanbeusekom I tested with 6.1.2 and now getCurrentPosition successfully returns a position while I have an active stream going. Thanks for the update!

Awesome thanks a lot for testing and the feedback!

I get this error:

E/flutter (18928): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: The location service on the device is disabled.
E/flutter (18928): #0 MethodChannelGeolocator._handlePlatformException (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:183:9)
E/flutter (18928): #1 MethodChannelGeolocator.getPositionStream. (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:162:11)
E/flutter (18928): #2 _invokeErrorHandler (dart:async/async_error.dart:16:24)
E/flutter (18928): #3 _HandleErrorStream._handleError (dart:async/stream_pipe.dart:282:9)
E/flutter (18928): #4 _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:161:13)
E/flutter (18928): #5 _rootRunBinary (dart:async/zone.dart:1214:47)
E/flutter (18928): #6 _CustomZone.runBinary (dart:async/zone.dart:1107:19)
E/flutter (18928): #7 _CustomZone.runBinaryGuarded (dart:async/zone.dart:1013:7)
E/flutter (18928): #8 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:376:15)
E/flutter (18928): #9 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:394:16)
E/flutter (18928): #10 _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:294:7)
E/flutter (18928): #11 _ForwardingStreamSubscription._addError (dart:async/stream_pipe.dart:132:11)
E/flutter (18928): #12 _ForwardingStream._handleError (dart:async/stream_pipe.dart:97:10)
E/flutter (18928): #13 _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:161:13)
E/flutter (18928): #14 _rootRunBinary (dart:async/zone.dart:1222:13)
E/flutter (18928): #15 _CustomZone.runBinary (dart:async/zone.dart:1107:19)
E/flutter (18928): #16 _CustomZone.runBinaryGuarded (dart:async/zone.dart:1013:7)
E/flutter (18928): #17 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:376:15)
E/flutter (18928): #18 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:394:16)
E/flutter (18928): #19 _DelayedError.perform (dart:async/stream_impl.dart:622:14)
E/flutter (18928): #20 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:730:11)
E/flutter (18928): #21 _PendingEvents.schedule. (dart:async/stream_impl.dart:687:7)
E/flutter (18928): #22 _rootRun (dart:async/zone.dart:1182:47)
E/flutter (18928): #23 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (18928): #24 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (18928): #25 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1037:23)
E/flutter (18928): #26 _rootRun (dart:async/zone.dart:1190:13)
E/flutter (18928): #27 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (18928): #28 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (18928): #29 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1037:23)
E/flutter (18928): #30 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (18928): #31 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter (18928):

@ViniciusDS you will receive this error when the location services on the device are disabled (or are being disabled during while the stream is active).

You can use the Geolocator.isLocationServiceEnabled() to manually check if the location services are enabled or not. And while listening to the getPositionStream you should always handle the LocationServiceDisabledException since users can simply turn off the location services while you are listening to the stream.

Hope this helps. If you keep experiencing issues, please create a separate issue and I will be able to help you there.

I'm running into a similar problem with getCurrentPosition Timing out except mine is crashing entirely here.

return await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.bestForNavigation);

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: TimeoutException after 0:00:00.000000: Future not completed

I'm using the latest version. We'd updated from version geolocator: ^5.1.3 and can confirm we were using forceAndroidLocationManager. Any help would be greatly appreciated. We're going to attempt to roll back for now as this is preventing our app from working at all currently.

@billysmt, can you please run a flutter pub get command. I have introduced a bug in version 6.1.2 and made an hotfix this morning. More information can be found in issues #567 and #566.

Had to run a flutter pub upgrade, but I see the update now.

geolocator:
dependency: "direct main"
description:
name: geolocator
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.2"
geolocator_platform_interface:
dependency: transitive
description:
name: geolocator_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.6+1"

Looks to be working. I have it wrapped as follows just in case but it's not tripping on the error anymore.

Position geoPos;
try {
  geoPos = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.bestForNavigation);
} catch(e, stackTrace) {
  geoPos = await Geolocator.getLastKnownPosition();
  await ErrorUtil.reportError(e, stackTrace);
}
return geoPos;

Thanks for quick update!

Was this page helpful?
0 / 5 - 0 ratings