getCurrentLocation method
//////////// //////////// Retrieves the current location with comprehensive permission handling
Implementation
// II.C - Location Retrieval Methods
///////////////
/// Retrieves the current location with comprehensive permission handling
Future<geo.Position?> getCurrentLocation() async {
try {
// First check if location services are enabled
bool serviceEnabled = await geo.Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
final errorMsg = 'Location services are disabled. Please enable GPS in your device settings.';
debugPrint('[LOCATION] $errorMsg');
_showErrorMessage(errorMsg);
return null;
}
// Check and request location permissions
geo.LocationPermission permission = await _checkAndRequestPermission();
debugPrint("[DEBUG] Permission status after check/request: $permission at ${DateTime.now()}");
// Verify permission is granted
if (permission == geo.LocationPermission.denied ||
permission == geo.LocationPermission.deniedForever) {
final errorMsg = permission == geo.LocationPermission.denied
? 'Location permission denied. Please grant location access for navigation.'
: 'Location permissions permanently denied. Please enable in app settings.';
debugPrint('[LOCATION] $errorMsg');
_showErrorMessage(errorMsg);
return null;
}
// Get current position with high accuracy
debugPrint('[LOCATION] Getting current position with accuracy: $_locationAccuracy');
final position = await geo.Geolocator.getCurrentPosition(
desiredAccuracy: _locationAccuracy,
timeLimit: const Duration(seconds: 10),
);
debugPrint('[LOCATION] Position obtained: lat=${position.latitude}, lng=${position.longitude}, accuracy=${position.accuracy}m');
_lastKnownLocation = position;
return position;
} on PlatformException catch (e) {
final errorMsg = _getReadableLocationError(e);
debugPrint('[LOCATION] Platform error getting current location: ${e.code} - ${e.message}');
_showErrorMessage(errorMsg);
return null;
} catch (e) {
debugPrint('[LOCATION] Error obtaining current location: $e');
_showErrorMessage('Unable to get current location: $e');
return null;
}
}