wagnerdelima
wagnerdelima

Reputation: 360

Flutter geolocator package not retrieving location

I've open an issue on the geolocator repository https://github.com/BaseflowIT/flutter-geolocator/issues/199

It entails the geolocator package not retrieving the location. They recently released a new version 3.0.0 and after that the I have had only aftermath.

I am using the correct dependencies:

dependencies:
 geolocator: '^3.0.0'

targetSdkVersion 28 and compileSdkVersion 28

Flutter doctor gives me this:

[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.3 18D109, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[✓] iOS toolchain - develop for iOS devices (Xcode 10.1)
[✓] Android Studio (version 3.2)
[✓] IntelliJ IDEA Community Edition (version 2018.2.5)
[✓] Connected device (1 available)

• No issues found!

Once I call await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high); the code just doesn't return anything and I have this output in terminal:

I/DynamiteModule( 4233): Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:221 I/DynamiteModule( 4233): Selected remote version of com.google.android.gms.maps_dynamite, version >= 221 V/DynamiteModule( 4233): Dynamite loader version >= 2, using loadModule2NoCrashUtils W/System ( 4233): ClassLoader referenced unknown path: W/System ( 4233): ClassLoader referenced unknown path: /data/user_de/0/com.google.android.gms/app_chimera/m/00000030/n/armeabi-v7a W/System ( 4233): ClassLoader referenced unknown path: /data/user_de/0/com.google.android.gms/app_chimera/m/00000030/n/armeabi I/Google Maps Android API( 4233): Google Play services client version: 12451000 I/Google Maps Android API( 4233): Google Play services package version: 15090018 W/DynamiteModule( 4233): Local module descriptor class for com.google.android.gms.googlecertificates not found. I/DynamiteModule( 4233): Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4 I/DynamiteModule( 4233): Selected remote version of com.google.android.gms.googlecertificates, version >= 4 W/System ( 4233): ClassLoader referenced unknown path: /data/user_de/0/com.google.android.gms/app_chimera/m/0000002f/n/armeabi-v7a W/System ( 4233): ClassLoader referenced unknown path: /data/user_de/0/com.goo`gle.android.gms/app_chimera/m/0000002f/n/armeabi

I have spent a considerable amount of time on this. I am new to flutter and know that I may be missing a small thing to make it work.

Upvotes: 5

Views: 21425

Answers (13)

Ray S
Ray S

Reputation: 1

In the android manifest file, I only had the following permission:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Geolocator.getCurrentPosition(...) started working after I also added the ACCESS_FINE_LOCATION.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Also, I had the forceLocationManager set to true. I hope it helps.

Upvotes: 0

Anurag Dhunna
Anurag Dhunna

Reputation: 574

Apple doesn't allow multiple active calls to the location stream.

Solution:

Future<Position>? _positionFuture;

Future<Position> getLocation() async {
  if (_positionFuture != null) {
    // If a future is already in progress, return it directly
    return _positionFuture!;
  } else {
    // If no future is in progress, start a new one
    Completer<Position> completer = Completer<Position>();
    _positionFuture = completer.future;

    // Fetch the current position
    try {
      Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.medium,
      );
      // Complete the future with the fetched position
      completer.complete(position);
    } catch (e) {
      // Handle any errors and complete the future with an error
      completer.completeError(e);
    } 
    // Wait for the future to complete and then return its result
    return await _positionFuture!;
  }
}

Upvotes: 0

Jeremie Houet
Jeremie Houet

Reputation: 343

I had this problem until I tried to set the accuracy to low. Then it worked. I believe you may need extra permissions for high accuracy location. It's funny only the low accuracy works for me, none of the others. Also, the forceAndroidLocationManager has to be set to false for it to work on my IOS emulator.

This code worked for me:

 Geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.low,
            forceAndroidLocationManager: false)
        .then((currloc) {})

Upvotes: 1

pableiros
pableiros

Reputation: 16080

If you are using the iOS Simulator, you need to set the Location different from None in order the getCurrentPosition method returns the position:

enter image description here

Upvotes: 6

billybutcher
billybutcher

Reputation: 1

I had the same problem. I was able to solve it by:

  1. replacing current API key with new one
  2. clearing out all the minor bugs in file, e.g. writing remaining @override functions, adding 'const' and 'final' keywords etc. Try these, it might help.

Upvotes: 0

Muhammad Shafique
Muhammad Shafique

Reputation: 609

I had the same issue i.e it was not returning anything but keep fecthing location so i just changed from this

forceAndroidLocationManager: true

to this

forceAndroidLocationManager: false
Geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.bestForNavigation,
            forceAndroidLocationManager: false)
        .then((position) {
      print(position);
    });

Now its working fine. The problem is in new update because in the old package it was working fine

Upvotes: 1

Vikas
Vikas

Reputation: 151

This solution will work, I was facing issue with Android:

Geolocator geoLocator = Geolocator()..forceAndroidLocationManager = true;
Position position = await geoLocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
print(position);

Upvotes: 0

Abdulalim Elmozogi
Abdulalim Elmozogi

Reputation: 29

I was facing the same issue now it works, I've just added forceAndroidLocationManager: true to the Geolocator.getCurrentPosition.

Position position = await 
    Geolocator.getCurrentPosition(forceAndroidLocationManager: true,
    desiredAccuracy: LocationAccuracy.lowest);

Upvotes: 2

Supun Dewapriya
Supun Dewapriya

Reputation: 763

On iOS you'll need to add the following entries to your Info.plist file (located under ios/Runner) in order to access the device's location. Simply open your Info.plist file and add the following:

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>

Upvotes: 3

JBSan
JBSan

Reputation: 11

I was struggling with this problem for about 2 days. After that, I figured out this (Mac):

  1. In Simulator iOS did not work anyway;
  2. Connect an iOS real device After open iOS module in XCode as follow
  3. Right-click on folder project
  4. Choose Flutter->Open iOS module in XCode
  5. On XCode, Select TAGETS -> Runner
  6. In Signing & Capabilities:
  7. Set the Team
  8. Change the Bundle Identifier to a unique one (ex.:co.test.app456)
  9. Run your app in XCode (it will work)
  10. Close XCode and go back to your project in Flutter
  11. Select the real iOS device and run your app

Upvotes: 1

Jabir Ishaq
Jabir Ishaq

Reputation: 177

First things first, check the device you are using and your Flutter version. i had same issue with only one device that is Google's Nexus 6P and Flutter Version 1.9.1+hotfix.6. The problem is with the package "geolocator" because all other devices are working fine.

I recommend new_geolocation. As it resolved the issue for all devices so far.

Secondly, how i used it, the readme file here didnt help, so went through the example project on Github here, used the same methods of this example for exact results.

I hope it will help.

Upvotes: 0

Rohit Islam
Rohit Islam

Reputation: 83

I was facing the same issue. But my app started running after adding this.

[ Geolocator geolocator = Geolocator()..forceAndroidLocationManager = true; ]

Hope it will work.

    Future<void> getCurrentLocation() async {
    try {
      Geolocator geolocator = Geolocator()..forceAndroidLocationManager = true;

      Position position = await Geolocator().getCurrentPosition(
        desiredAccuracy: LocationAccuracy.best,
      );

      return position;
    } catch (err) {
      print(err.message);
    }
  }

All the best.

Upvotes: 4

Asfos22
Asfos22

Reputation: 319

Hey @wagnerdelima had same challenge and l solved by the following :

Change the targetSdkVersion 28 and compileSdkVersion 28 to targetSdkVersion 27 and compileSdkVersion 27 and change to geolocator: '^3.0.0' to geolocator: ^2.1.1 as below:

dependencies: flutter: sdk: flutter geolocator: ^2.1.1 permission_handler: "2.1.2" google_api_availability: "1.0.4"

This was as a result of the caret ^, it is taking the google_api_availability latest, which is migrated to android x.

All the best !!

Upvotes: 1

Related Questions