Skip to main content

Overview

The LocationRepository 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

class LocationRepository(private val context: Context)
context
Context
required
Android Context used for checking permissions and accessing location services

Methods

hasPermission

Checks if the app has location permissions.
fun hasPermission(): Boolean
Boolean
Boolean
Returns true if either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission is granted
Example:
val locationRepository = LocationRepository(context)
if (locationRepository.hasPermission()) {
    // Proceed with location access
} else {
    // Request location permissions
}
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.
suspend fun getLatLng(): Pair<Double, Double>
Pair<Double, Double>
Pair<Double, Double>
A pair containing latitude and longitude coordinates
Example:
val locationRepository = LocationRepository(context)
try {
    val (latitude, longitude) = locationRepository.getLatLng()
    println("Location: $latitude, $longitude")
} catch (e: IllegalStateException) {
    // Handle location error
    println("Error: ${e.message}")
}

Location Retrieval Strategy

The getLatLng method attempts to get location in the following order:
  1. Last Known Location: First tries to retrieve the last known location from cache
  2. Current Location: If no cached location exists, requests a fresh high-accuracy location
  3. Error Handling: Throws descriptive errors if location is unavailable
// Priority settings
Priority.PRIORITY_HIGH_ACCURACY // Used for current location requests

Error Handling

The method throws IllegalStateException with descriptive messages in the following cases:
SecurityException
IllegalStateException
Message: “Permiso de ubicación no concedido.” (Location permission not granted)Thrown when location permissions have not been granted
Location Unavailable
IllegalStateException
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
Generic Error
IllegalStateException
Message: “Error obteniendo ubicación: error message” (Error getting location)Thrown for any other errors during location retrieval
Example with error handling:
val locationRepository = LocationRepository(context)

if (!locationRepository.hasPermission()) {
    // Request permissions first
    requestLocationPermissions()
    return
}

try {
    val (lat, lng) = locationRepository.getLatLng()
    // Use coordinates
    displayLocationOnMap(lat, lng)
} catch (e: IllegalStateException) {
    when {
        e.message?.contains("Permiso") == true -> {
            // Permission denied
            showPermissionError()
        }
        e.message?.contains("GPS") == true -> {
            // GPS disabled or no signal
            showGpsError()
        }
        else -> {
            // Other error
            showGenericError(e.message)
        }
    }
}

Required Permissions

Add at least one of these permissions to your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Dependencies

This repository requires Google Play Services Location:
implementation 'com.google.android.gms:play-services-location:latest_version'

Source Location

com.demodogo.ev_sum_2.data.repositories.LocationRepository

Build docs developers (and LLMs) love