Flutter-geolocator: Android Pie (API 28) Geolocator().getCurrentPosition() does not return

Created on 2 Nov 2018  ·  39Comments  ·  Source: Baseflow/flutter-geolocator

🐛 Bug Report

When I run Geolocator().getCurrentPosition() on Android Emulator with Android Pie (API 28) call is not returning at all.

I run the same code on API 27 location returns fine. I tried to create new emulator, nothing changes.

Configuration

Android Emulator with Android Pie (API 28)

Version: 2.1.0

Platform:

  • [ ] :iphone: iOS
  • [X ] :robot: Android
android

Most helpful comment

I have the same problem and I set a timeout to function. When timeout occurred I call getLastKnownPosition()

Geolocator().getCurrentPosition().timeout(Duration(seconds: 10)).then((position) {
      //getting position without problems
      print(position);
    }).catchError((error) {
      if (error is TimeoutException) {
        Geolocator().getLastKnownPosition().then((position) {
          //getting last known position
          print(position);
        }).catchError((error) {
          //handle the exception
        });
      } else {
        //handle the exception
      }
    });

You could also do the same with stream

All 39 comments

mee too, whats wrong? :(

Same here!

@savjolovs if I debug, the debugger stops on the Geolocator().getCurrentPosition(). Breakpoints after the mehtod are not getting hit. Can you confirm this behaviour?

Same behaviour here till push some coordinates using extended controls.
Same behaviour with one of two phisycal devices. First with android 5 - works fine, another with 6 - no response till fakegps location engaged.

+1 here

Anyone an idea whats the cause? This feature is breaking everything, did anyone tried another plugin?

Checking the obvious, for those with the problem, do you have a gps signal for it to get a location and have moved ? Just wondering if it's sat there waiting for some new location before it updates ?

@ibrierley seems like can't get GPS signal with or without moving, but at the same time different apps like google maps have no problems to locate current position. lastKnownPosition returns location for about couple hundred meters far away. With android 5 don't have problems at all, even not asked for the location permission (but not sure, may be i missed this at debug)

Same issues on Android oreo except you close and open the app again

I've been fiddling with an Android Wear watch and had a lot of problems with GPS, but I seem to have it working mostly (it can time out after a long time, but I think that's a different issue).

I'm not sure what exactly has got it working, as I've tried lots of things, but maybe someone can check these off and see if they help.

0) Make sure your device can get a GPS signal, so outside or feed it locations via the emulator (I feed it a GPX route I downloaded to test).

1) Make sure the screen is on. GPS may stop returning results if it's in minimal mode whatever it's called. I force the screen on when needed with Screen package and Screen.keepOn(true); If the screen is off, it seems to hold and buffer a set of locations until the screen is back on.

2) These are my permissions in AndroidManifest.xml, maybe worth a try

```




Note especially the very last one, as I think there was a change along the way in one version, and it seemed to help. You may not want the wake_lock for battery reasons, but it may be useful to try this to eliminate where the problem is.

3) I had oddities with the Stream timing out, so I ended up starting the stream with geolocator.getPositionStream but not actually using it. With the stream running, I'm calling getCurrentPosition( desiredAccuracy: LocationAccuracy.high ) based on a timer. I think this isn't needed now, as I think my original problem was the screen being off, but it may be worth trying just out of interest.

If someone tries the above and can't get it working, can you create a minimal flutter app, just with the location stuff, and I'll see if it fails the same on mine. It is a lot more painful to get going than it should :/.

Thanks @ibrierley for your information, will try as soon as possible and report about my experience solving the issue.

Did some research today with an additional tools like GPS test on physical devices.
So there is no response when sat is not available by GPS test (0 connected 1 available because of clouds and big buildings around) even google map or other map services are works.
No response when sat is online but defined distance in location options, but not 100% in this case - need some kind of gps drift to have an initial coordinates.
I did some trick with lastknownlocation to position over there to initiate app that require current location before actual location data available.
Probably would be a great solution to have some kind of flag like isReady with possible callback in the Geolocator that indicate is GPS service is ready to offer location data through stream.

Has anyone tried for example this: https://github.com/Lyokone/flutterlocation, or this: https://github.com/Lyokone/flutterlocation ? So we can exlude errors that are not related to this package.

I've tried that, and ended up with this one (that's not to say it's not
worth a try though!). All of them have 'some' problems, I think really
because Android has some things that aren't obvious.

Typically I've found when a location doesn't get returned on any of the
packages), it's because of a none-flutter issue (eg the gps has gone into
sleep mode, or no gps signal).

On Thu, Nov 15, 2018 at 7:26 PM HerrNiklasRaab notifications@github.com
wrote:

Has anyone tried for example this:
https://github.com/Lyokone/flutterlocation, or this:
https://github.com/Lyokone/flutterlocation ? So we can exlude errors that
are not related to this package.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/BaseflowIT/flutter-geolocator/issues/135#issuecomment-439160446,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADuG9WRbZuJYvi5N_2xrrku-Jurk6trIks5uvb_SgaJpZM4YKDSm
.

Does it work with the screen off out of interest ?

On Thu, Nov 15, 2018 at 8:16 PM Chris Scott notifications@github.com
wrote:

Anyone try mine?
flutter_background_geolocation
https://pub.dartlang.org/packages/flutter_background_geolocation#-readme-tab-


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/BaseflowIT/flutter-geolocator/issues/135#issuecomment-439175841,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADuG9f5uvZK0lDnYecMoFZhJnF0ylz6Pks5uvcuygaJpZM4YKDSm
.

It works even if you terminate your app (stopOnTerminate: false) or reboot the device (startOnBoot: true).

I am facing the same problem, which is never returning from getCurrentPosition.
I have the following code:
position = await Geolocator() .getCurrentPosition(desiredAccuracy: LocationAccuracy.best); setState(() { _position = position; }); if (position == null) Fluttertoast.showToast( msg: 'Position is null', toastLength: Toast.LENGTH_SHORT); else Fluttertoast.showToast( msg: 'Got Position', toastLength: Toast.LENGTH_SHORT);

The permissions are:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.hardware.location.gps" /> <uses-permission android:name="android.hardware.location.network" /> <uses-permission android:name="android.permission.WAKE_LOCK" />.
When I run the program, the location indicator appears on the top on the screen and stay still until I close the program.
Any help, please?

Same happen, getCurrentPosition does not work using API 23.
Any work around for this or any alternative package working for getting current posiiton?

It's a bit tricky. I could make it work now by launching Google Map app first and let it ask for permission to enable GPS when tap my location. After that getCurrentPosition suddenly work like a charm.

In my case, I can't retrieve location with any API, Not sure if this can solve your problems but
I tried removing the NOT operator here, added return statement and it worked. https://github.com/BaseflowIT/flutter-geolocator/blob/master/android/src/main/java/com/baseflow/flutter/plugin/geolocator/tasks/LocationUpdatesUsingLocationManagerTask.java#L52

// If we are listening to multiple location updates we can go ahead
// and report back the last known location (if we have one).
if(mStopAfterFirstLocationUpdate && mBestLocation != null) {
reportLocationUpdate(mBestLocation);
return;
}

I just stumbled across this as I needed to get a really quick fix out after I used another plugin which let me down in production. I can confirm that I have tested this on API 23 (physical device), API 24 (physical device) and API 28 (physical device) and didn't have an issue with it. On API 23 I was asked if I wanted to help Google improve location etc etc and just said yes, it loaded the location after about 20 or so seconds. In the past, I've had real problems getting emulators to return locations (native Android development), so could it just be an emulator problem?

I have the same problem and I set a timeout to function. When timeout occurred I call getLastKnownPosition()

Geolocator().getCurrentPosition().timeout(Duration(seconds: 10)).then((position) {
      //getting position without problems
      print(position);
    }).catchError((error) {
      if (error is TimeoutException) {
        Geolocator().getLastKnownPosition().then((position) {
          //getting last known position
          print(position);
        }).catchError((error) {
          //handle the exception
        });
      } else {
        //handle the exception
      }
    });

You could also do the same with stream

@NMateu Thanks for your contribution. That was very helpful!

I had the same problem on emulator, but .forceAndroidLocationManager = true; fixed it for me. Seems that fused location manager sometimes gets in the way.

any news on this?

Did anybody manage to solve the problem? I really need to get this done, folks!

The same problem, it works well on ios device, but cannot get the location on android these days.

yes, I can actually get the address with the placement placemarkFromAddress method. But the location itself is broken.

same issue here

void _getUserLocation() async {
Geolocator geolocator = Geolocator()..forceAndroidLocationManager = true;
GeolocationStatus geolocationStatus = await geolocator.checkGeolocationPermissionStatus();
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
List placemark = await Geolocator()
.placemarkFromCoordinates(position.latitude, position.longitude);
print("-----------------");
print(placemark[0].position);
print(geolocator.toString());
print(geolocationStatus.toString());
setState(() {
_initialPosition = LatLng(position.latitude, position.longitude);

  locationController.text = placemark[0].name;
});

}

its showing the wrong location.

I/flutter ( 7363): Lat: 37.421999899999996, Long: -122.08405750000001
I/flutter ( 7363): Instance of 'Geolocator'
I/flutter ( 7363): GeolocationStatus.granted

void _getUserLocation() async {
Geolocator geolocator = Geolocator()..forceAndroidLocationManager = true;
GeolocationStatus geolocationStatus = await geolocator.checkGeolocationPermissionStatus();
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
List placemark = await Geolocator()
.placemarkFromCoordinates(position.latitude, position.longitude);
print("-----------------");
print(placemark[0].position);
print(geolocator.toString());
print(geolocationStatus.toString());
setState(() {
_initialPosition = LatLng(position.latitude, position.longitude);

  locationController.text = placemark[0].name;
});

}

Works on emulator with this snippet
Geolocator()..forceAndroidLocationManager = true;

This package worked like charm for everything

(https://github.com/alfanhui/new_geolocation)

I was facing the same problem with few android devices. But I have found a soution for it -

All you need to do is set the value of Geolocator()..forceAndroidLocationManager = true.

final Geolocator geolocator = Geolocator()..forceAndroidLocationManager = true;

Solved in version 6.0.0

Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.low,forceAndroidLocationManager: true);
It fixed my problems

I am getting a error in line forceAndroidLocationManager its showing red line and error is that ....."The setter 'forceAndroidLocationManager' isn't defined for the type 'Geolocator'.
Try importing the library that defines 'forceAndroidLocationManager', correcting the name to the name of an existing setter, or defining a setter or field named 'forceAndroidLocationManager'.".........plss can someone help me with this

@aditikumar-png which version of the geolocator are you using? You probably updated to version 6.x recently. This version has a breaking change. We moved the Geolocator().forceAndroidLocationManager property to a named parameter on the getCurrentPosition and getPositionStream methods.

This means you can now use the geolocator like this:

final position = await Geolocator.getCurrentPosition(forceAndroidLocationManager: true);

instead of the old:

final geolocator = Geolocator();
geolocator.forceAndroidLocationManager = true;

final position = await geolocator.getCurrentPosition();

@aditikumar-png which version of the geolocator are you using? You probably updated to version 6.x recently. This version has a breaking change. We moved the Geolocator().forceAndroidLocationManager property to a named parameter on the getCurrentPosition and getPositionStream methods.

This means you can now use the geolocator like this:

final position = await Geolocator.getCurrentPosition(forceAndroidLocationManager: true);

instead of the old:

final geolocator = Geolocator();
geolocator.forceAndroidLocationManager = true;

final position = await geolocator.getCurrentPosition();

Thank you so much!!!!! u solved my problem in seconds i was struggling with this since 9 hrs

Was this page helpful?
0 / 5 - 0 ratings