Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Location not updating using LocationSettings #1663

Open
3 of 8 tasks
Rensykes opened this issue Mar 20, 2025 · 2 comments
Open
3 of 8 tasks

[Bug]: Location not updating using LocationSettings #1663

Rensykes opened this issue Mar 20, 2025 · 2 comments
Labels
status: needs more info We need more information before we can continue work on this issue.

Comments

@Rensykes
Copy link

Please check the following before submitting a new issue.

Please select affected platform(s)

  • Android
  • iOS
  • Linux
  • macOS
  • Web
  • Windows

Steps to reproduce

When I try to fetch location on Android (using the emulator), if I simply use await Geolocator.getCurrentPosition() everything works fine. If I change the location in the emulator, the location that Geolocator fetches, changes.

If I Instead use either this code:

import 'package:geolocator/geolocator.dart';
import 'package:geolocator_android/geolocator_android.dart';

 LocationSettings locationSettings  = AndroidSettings(
        accuracy: LocationAccuracy.best,
        forceLocationManager: true,
        timeLimit: Duration(minutes: 2),
    );

    Position? position = await Geolocator.getCurrentPosition(
      locationSettings: locationSettings,
    );
import 'package:geolocator/geolocator.dart';
import 'package:geolocator_android/geolocator_android.dart';

     LocationSettings locationSettings = LocationSettings(
       accuracy: LocationAccuracy.reduced,
       distanceFilter: 500,
       timeLimit: Duration(seconds: 30),
     );

    Position? position = await Geolocator.getCurrentPosition(
      locationSettings: locationSettings,
    );
     Position position = await Geolocator.getCurrentPosition(
       desiredAccuracy: LocationAccuracy.low,
      forceAndroidLocationManager: true,
       timeLimit: Duration(seconds: 30)
     );

The location is retrieved almost instantly but it's the old one. Meaning that if I change it from the emulator, I don't get the new one.

Expected results

Location changes according to what is set in the emulator

[log] Saving country visit
[log] 📝 Log Added: Status - success, Country - IT
...
[log] Saving country visit
[log] 📝 Log Added: Status - success, Country - CH

Actual results

Location does not change after having been fetched once

[log] Saving country visit
[log] 📝 Log Added: Status - success, Country - IT
...
[log] Saving country visit
[log] 📝 Log Added: Status - success, Country - IT

Code sample

Here's the complete class (full of commented stuff)
import 'dart:developer';
import 'dart:io';
import 'package:geolocator/geolocator.dart';
import 'package:geolocator_android/geolocator_android.dart';
import 'package:geocoding/geocoding.dart';
import 'package:app_settings/app_settings.dart';
import 'package:permission_handler/permission_handler.dart';

class LocationService {
  // Method to open location settings
  static Future<void> openLocationSettings() async {
    if (Platform.isAndroid) {
      await AppSettings.openAppSettings();
    } else if (Platform.isIOS) {
      await openAppSettings();
    }
  }

  // You might also want to add a method to check location permissions
  static Future<bool> checkLocationPermission() async {
    PermissionStatus status = await Permission.locationAlways.status;
    return status.isGranted;
  }

  // Request permissions
  static Future<bool> requestPermission() async {
    LocationPermission permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
    }

    if (permission == LocationPermission.deniedForever) {
      log("Location permission permanently denied. Redirecting to settings...");
      await Geolocator.openAppSettings(); // Open settings page
      return false;
    }

    return permission == LocationPermission.always ||
        permission == LocationPermission.whileInUse;
  }

  // Get current country
  static Future<String?> getCurrentCountry() async {
/*     LocationSettings locationSettings;
    if (defaultTargetPlatform == TargetPlatform.android) {
      locationSettings = AndroidSettings(
        accuracy: LocationAccuracy.lowest,
        distanceFilter: 100,
        forceLocationManager: true,
        intervalDuration: const Duration(seconds: 15),
        //(Optional) Set foreground notification config to keep the app alive
        //when going to the background
        foregroundNotificationConfig: const ForegroundNotificationConfig(
          notificationText:
              "Example app will continue to receive your location even when you aren't using it",
          notificationTitle: "Running in Background",
          enableWakeLock: true,
        ),
      );
    } else if (defaultTargetPlatform == TargetPlatform.iOS ||
        defaultTargetPlatform == TargetPlatform.macOS) {
      locationSettings = AppleSettings(
        accuracy: LocationAccuracy.lowest,
        activityType: ActivityType.other,
        distanceFilter: 100,
        pauseLocationUpdatesAutomatically: true,
        // Only set to true if our app will be started up in the background.
        showBackgroundLocationIndicator: true,
      );
    } else {
      locationSettings = LocationSettings(accuracy: LocationAccuracy.lowest);
    } */


    // // Simplify location settings for background operation
    // LocationSettings locationSettings = LocationSettings(
    //   accuracy: LocationAccuracy.reduced,
    //   distanceFilter: 500,
    //   timeLimit: Duration(seconds: 30), // Avoid hanging
    // );

    LocationSettings locationSettings  = AndroidSettings(
        accuracy: LocationAccuracy.best,
        forceLocationManager: true,
        timeLimit: Duration(minutes: 2),
        // foregroundNotificationConfig: const ForegroundNotificationConfig(
        //   notificationText:
        //       "Example app will continue to receive your location even when you aren't using it",
        //   notificationTitle: "Running in Background",
        //   enableWakeLock: false,
        // ),
    );
    bool hasPermission = await requestPermission();
    if (!hasPermission) return null;

    Position? position = await Geolocator.getCurrentPosition(
      locationSettings: locationSettings,
    );
    // Position position = await Geolocator.getCurrentPosition(
    //   desiredAccuracy: LocationAccuracy.low,
    //   forceAndroidLocationManager: true, // Important for background operation
    //   timeLimit: Duration(seconds: 30)
    // );

    // Position position = await Geolocator.getCurrentPosition();

    List<Placemark> placemarks = await placemarkFromCoordinates(
      position.latitude,
      position.longitude,
    );

    return placemarks.isNotEmpty ? placemarks.first.isoCountryCode : null;
  }
}

Screenshots or video

Screenshots or video demonstration

[Upload media here]

Version

13.0.2

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.29.0, on Microsoft Windows [Versione 10.0.22631.5039], locale it-IT)
[√] Windows Version (11 Home 64 bit, 23H2, 2009)
[!] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/to/windows-android-setup for more details.
[√] Chrome - develop for the web
[X] Visual Studio - develop Windows apps
    X Visual Studio not installed; this is necessary to develop Windows apps.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2024.2)
[√] VS Code (version 1.98.2)
[√] Connected device (4 available)
[√] Network resources
@TimHoogstrate
Copy link
Contributor

Dear @Rensykes,

I am not sure why this happens, but the location is retrieved from the "OS" or Android in this case. Can you try to test this on a real device and see if you get a different result? The emulator is I also once saw something similar and you needed to open "google maps" first before it was properly applied.

Kind regards,

@TimHoogstrate TimHoogstrate added the status: needs more info We need more information before we can continue work on this issue. label Mar 24, 2025
@Rensykes
Copy link
Author

Hello @TimHoogstrate.
I tried it with a real device and I had the same issue (I did it before opening the issue).
I don't have much info about the device since Im out for a business trip and that's not my personal device, but if you feel like you need them, I can tell you once I'm back

@github-actions github-actions bot removed the status: needs more info We need more information before we can continue work on this issue. label Mar 26, 2025
@TimHoogstrate TimHoogstrate added the status: needs more info We need more information before we can continue work on this issue. label Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs more info We need more information before we can continue work on this issue.
Projects
None yet
Development

No branches or pull requests

2 participants