Maps and Location in Android
Step 1: Get a Maps API Key
- Create a project in Google Cloud Console and enable Maps SDK for Android.
- Create an API key and restrict it to your app's package name and SHA-1 certificate.
- Store the key outside source control, for example in
local.propertieswith the Secrets Gradle Plugin.
xml
<application>
<meta-data android:name="com.google.android.geo.API_KEY" android:value="${MAPS_API_KEY}" />
</application>
kotlin
implementation(libs.maps.compose)
implementation(libs.play.services.maps)
implementation(libs.play.services.location)
Step 2: Show a Map
kotlin
@Composable
fun CityMap() {
val paris = LatLng(48.8566, 2.3522)
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(paris, 11f)
}
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = cameraPositionState,
properties = MapProperties(isMyLocationEnabled = false),
uiSettings = MapUiSettings(zoomControlsEnabled = true)
) {
Marker(
state = MarkerState(position = paris),
title = "Paris",
snippet = "Capital of France"
)
}
}
Step 3: Location Permission
xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
kotlin
@Composable
fun LocationScreen() {
val context = LocalContext.current
var location by remember { mutableStateOf<Location?>(null) }
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result ->
val granted = result[Manifest.permission.ACCESS_FINE_LOCATION] == true ||
result[Manifest.permission.ACCESS_COARSE_LOCATION] == true
if (granted) fetchLocation(context) { location = it }
}
Column(Modifier.padding(16.dp)) {
Button(onClick = {
launcher.launch(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION))
}) { Text("Find me") }
location?.let { Text("Lat ${it.latitude}, Lng ${it.longitude}") }
}
}
@SuppressLint("MissingPermission")
fun fetchLocation(context: Context, onResult: (Location?) -> Unit) {
val client = LocationServices.getFusedLocationProviderClient(context)
client.getCurrentLocation(Priority.PRIORITY_BALANCED_POWER_ACCURACY, CancellationTokenSource().token)
.addOnSuccessListener(onResult)
}
Precise vs approximate: From Android 12, users can grant approximate location only. Ask for both permissions and design your feature to work with approximate results when possible.
Continuous Updates
kotlin
val request = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 5_000).build()
fun locationUpdates(client: FusedLocationProviderClient): Flow<Location> = callbackFlow {
val callback = object : LocationCallback() {
override fun onLocationResult(result: LocationResult) { result.lastLocation?.let { trySend(it) } }
}
client.requestLocationUpdates(request, callback, Looper.getMainLooper())
awaitClose { client.removeLocationUpdates(callback) }
}
Privacy: Request location only when needed, explain why, stop updates when the screen closes and avoid background location unless it is essential. Play review checks this closely.
Conclusion
Maps Compose shows a map in a few lines. Location needs runtime permissions, and good apps request them with a clear reason.
Quick Summary: Add an API key, show GoogleMap with Marker, request location permissions with a launcher and use the Fused Location client.

