You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have carefully read the documentation and verified I have added the required platform specific configuration.
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.
Position position =awaitGeolocator.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';
classLocationService {
// Method to open location settingsstaticFuture<void> openLocationSettings() async {
if (Platform.isAndroid) {
awaitAppSettings.openAppSettings();
} elseif (Platform.isIOS) {
awaitopenAppSettings();
}
}
// You might also want to add a method to check location permissionsstaticFuture<bool> checkLocationPermission() async {
PermissionStatus status =awaitPermission.locationAlways.status;
return status.isGranted;
}
// Request permissionsstaticFuture<bool> requestPermission() async {
LocationPermission permission =awaitGeolocator.checkPermission();
if (permission ==LocationPermission.denied) {
permission =awaitGeolocator.requestPermission();
}
if (permission ==LocationPermission.deniedForever) {
log("Location permission permanently denied. Redirecting to settings...");
awaitGeolocator.openAppSettings(); // Open settings pagereturnfalse;
}
return permission ==LocationPermission.always ||
permission ==LocationPermission.whileInUse;
}
// Get current countrystaticFuture<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 =awaitrequestPermission();
if (!hasPermission) returnnull;
Position? position =awaitGeolocator.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 =awaitplacemarkFromCoordinates(
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
The text was updated successfully, but these errors were encountered:
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.
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
Please check the following before submitting a new issue.
Please select affected platform(s)
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:
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)
Screenshots or video
Screenshots or video demonstration
[Upload media here]
Version
13.0.2
Flutter Doctor output
Doctor output
The text was updated successfully, but these errors were encountered: