Overview
TheLocationRepository class provides access to the device’s location using Google Play Services Fused Location Provider. It handles permission checks and retrieves both last known and current location.
Class Definition
Android Context used for checking permissions and accessing location services
Methods
hasPermission
Checks if the app has location permissions.Returns true if either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission is granted
The method checks for both
ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions. If either is granted, it returns true.getLatLng
Retrieves the device’s current latitude and longitude coordinates.A pair containing latitude and longitude coordinates
Location Retrieval Strategy
ThegetLatLng method attempts to get location in the following order:
- Last Known Location: First tries to retrieve the last known location from cache
- Current Location: If no cached location exists, requests a fresh high-accuracy location
- Error Handling: Throws descriptive errors if location is unavailable
Error Handling
The method throwsIllegalStateException with descriptive messages in the following cases:
Message: “Permiso de ubicación no concedido.” (Location permission not granted)Thrown when location permissions have not been granted
Message: “Ubicación no disponible (GPS apagado o sin señal).” (Location unavailable - GPS off or no signal)Thrown when neither last location nor current location could be retrieved
Message: “Error obteniendo ubicación: error message” (Error getting location)Thrown for any other errors during location retrieval
Required Permissions
Add at least one of these permissions to yourAndroidManifest.xml:
Dependencies
This repository requires Google Play Services Location:Source Location
com.demodogo.ev_sum_2.data.repositories.LocationRepository