Location services help your app find where the user is on the map. This lets your app give directions, show nearby places, or customize content based on location.
0
0
Location services in Android Kotlin
Introduction
Showing the user's current position on a map in a travel app.
Finding nearby restaurants or stores in a shopping app.
Tracking a delivery or ride in real time.
Providing weather updates based on the user's location.
Helping users check in or tag their location in social apps.
Syntax
Android Kotlin
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.lastLocation
.addOnSuccessListener { location ->
if (location != null) {
val latitude = location.latitude
val longitude = location.longitude
// Use latitude and longitude
}
}Use getFusedLocationProviderClient to get the location provider.
Always check if the location is not null before using it.
Examples
Get the last known location and print latitude and longitude.
Android Kotlin
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.lastLocation.addOnSuccessListener { location ->
if (location != null) {
println("Lat: ${location.latitude}, Lon: ${location.longitude}")
}
}Use Kotlin's safe call and let to handle nullable location.
Android Kotlin
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.lastLocation.addOnSuccessListener { location ->
location?.let {
val lat = it.latitude
val lon = it.longitude
// Use lat and lon here
}
}Sample App
This app asks for location permission. If allowed, it shows the user's last known latitude and longitude on the screen. If permission is denied or location is unavailable, it shows a message.
Android Kotlin
import android.Manifest import android.content.pm.PackageManager import android.os.Bundle import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import com.google.android.gms.location.LocationServices import android.widget.TextView class MainActivity : AppCompatActivity() { private lateinit var locationText: TextView private val requestPermissionLauncher = registerForActivityResult( ActivityResultContracts.RequestPermission() ) { isGranted: Boolean -> if (isGranted) { getLastLocation() } else { locationText.text = "Permission denied" } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) locationText = TextView(this).apply { textSize = 18f setPadding(16, 16, 16, 16) } setContentView(locationText) when { ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED -> { getLastLocation() } else -> { requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION) } } } private fun getLastLocation() { val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this) fusedLocationClient.lastLocation.addOnSuccessListener { location -> if (location != null) { locationText.text = "Latitude: ${location.latitude}\nLongitude: ${location.longitude}" } else { locationText.text = "Location not available" } } } }
OutputSuccess
Important Notes
Always request location permission at runtime for Android 6.0 and above.
Last known location might be null if location was never requested before.
For continuous updates, use location request APIs instead of just last location.
Summary
Location services let your app find the user's position.
Use FusedLocationProviderClient to get location easily.
Always handle permissions and null location safely.