Repository navigation
chore(kmp-demo): add shared multiplatform Location Flow demo #1038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dkhawk
merged 16 commits into
feat/experimental-kmp-module
from
feat/kmp-location-flow-demo
Oct 13, 2026
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fd22127
feat: introduce experimental KMP maps module wrapping Google Maps on …
kikoso 36c1c1b
feat: migrate iOS multiplatform map implementation to native Google M…
kikoso 73f6bc4
feat: add interactive maps demos to iOS app & upgrade Google Maps SDK…
kikoso f173701
fix(ios): declare DeveloperSecrets.swift output path & add BUILD_KMP.md
dkhawk fbae135
feat: consume multiplatform clustering from android-maps-utils KMP br…
kikoso 31d0370
ci: publish KMP maps-utils modules to mavenLocal before building
kikoso 7aed67a
fix: consume KMP maps-utils under its public artifactIds
kikoso 94a8178
build: consume the maps-utils 6.1.0-SNAPSHOT instead of a mavenLocal …
kikoso 57273b2
Merge remote-tracking branch 'origin/main' into feat/experimental-kmp…
kikoso 7c5cd5d
build: publish maps-compose-multiplatform with API checks, lint and i…
kikoso 9087d81
test: run the multiplatform clustering tests on the JVM and iOS, docu…
kikoso 8b43d3e
feat: shared camera state, map content and clustering in maps-compose…
kikoso 425cec8
feat(kmp-demo): add shared multiplatform Location Flow demo
dkhawk 06f7bc4
docs(kmp-demo): add TODO referencing android-maps-utils#1818 in Platf…
dkhawk c30ec6e
chore: merge feat/experimental-kmp-module into feat/kmp-location-flow…
dkhawk dfe76f6
chore(kmp-demo): address review feedback on permission handling, mani…
dkhawk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
maps-compose-multiplatform-demo/src/androidMain/AndroidManifest.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- | ||
| Copyright 2026 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
|
||
| <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | ||
| <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | ||
|
|
||
| </manifest> |
196 changes: 196 additions & 0 deletions
196
...tlin/com/google/maps/android/compose/multiplatform/demo/PlatformLocationSource.android.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.maps.android.compose.multiplatform.demo | ||
|
|
||
| import android.Manifest | ||
| import android.annotation.SuppressLint | ||
| import android.app.Activity | ||
| import android.content.Context | ||
| import android.content.ContextWrapper | ||
| import android.content.Intent | ||
| import android.content.pm.PackageManager | ||
| import android.location.LocationManager | ||
| import android.net.Uri | ||
| import android.provider.Settings | ||
| import androidx.activity.compose.rememberLauncherForActivityResult | ||
| import androidx.activity.result.contract.ActivityResultContracts | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.DisposableEffect | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import androidx.lifecycle.compose.LocalLifecycleOwner | ||
| import androidx.lifecycle.Lifecycle | ||
| import androidx.lifecycle.LifecycleEventObserver | ||
| import com.google.maps.android.location.LocationPriority | ||
| import com.google.maps.android.location.LocationSource | ||
| import com.google.maps.android.location.asLocationSource | ||
| import com.google.maps.android.location.coarseLocationEvents | ||
| import kotlinx.coroutines.flow.catch | ||
| import kotlinx.coroutines.flow.channelFlow | ||
| import kotlinx.coroutines.flow.merge | ||
|
|
||
| private val LOCATION_PERMISSIONS = arrayOf( | ||
| Manifest.permission.ACCESS_FINE_LOCATION, | ||
| Manifest.permission.ACCESS_COARSE_LOCATION, | ||
| ) | ||
|
|
||
| private fun Context.hasAnyLocationPermission(): Boolean = | ||
| checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || | ||
| checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED | ||
|
|
||
| private fun Context.hasFineLocationPermission(): Boolean = | ||
| checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED | ||
|
|
||
| private fun Activity.shouldShowLocationRationale(): Boolean = | ||
| shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) || | ||
| shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION) | ||
|
|
||
| private tailrec fun Context.findActivity(): Activity? = when (this) { | ||
| is Activity -> this | ||
| is ContextWrapper -> baseContext.findActivity() | ||
| else -> null | ||
| } | ||
|
|
||
| @SuppressLint("MissingPermission") | ||
| @Composable | ||
| internal actual fun rememberPlatformLocationController(): PlatformLocationController { | ||
| val context = LocalContext.current | ||
| val appContext = context.applicationContext | ||
| val activity = remember(context) { context.findActivity() } | ||
| val lifecycleOwner = LocalLifecycleOwner.current | ||
| var hasPermission by remember { mutableStateOf(appContext.hasAnyLocationPermission()) } | ||
|
|
||
| // Re-check location permission whenever the host lifecycle resumes (covering both the | ||
| // system permission dialog closing and returning from Android App Settings). | ||
| DisposableEffect(lifecycleOwner, appContext) { | ||
| val observer = LifecycleEventObserver { _, event -> | ||
| if (event == Lifecycle.Event.ON_RESUME) { | ||
| hasPermission = appContext.hasAnyLocationPermission() | ||
| } | ||
| } | ||
| lifecycleOwner.lifecycle.addObserver(observer) | ||
| onDispose { | ||
| lifecycleOwner.lifecycle.removeObserver(observer) | ||
| } | ||
| } | ||
|
|
||
| // TODO(https://github.com/googlemaps/android-maps-utils/issues/1818): Once | ||
| // LocationManager.asLocationSource() handles cached lastKnownLocation emission and | ||
| // coarse-permission/indoor fallback directly in android-maps-utils, simplify this to | ||
| // `remember(manager) { manager.asLocationSource() }` matching PlatformLocationSource.ios.kt. | ||
| val locationSource = remember(appContext) { | ||
| val manager = appContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager | ||
| LocationSource { intervalMs, minUpdateDistanceM, priority -> | ||
| channelFlow { | ||
| if (!appContext.hasAnyLocationPermission()) { | ||
| return@channelFlow | ||
| } | ||
|
|
||
| // Safely query cached locations across providers; wrap each call in runCatching | ||
| // so that coarse-only ("Approximate") permission or a mid-flight revocation | ||
| // never throws an unhandled SecurityException. | ||
| val lastKnown = runCatching { | ||
| manager.getLastKnownLocation(LocationManager.GPS_PROVIDER) | ||
| }.getOrNull() | ||
| ?: runCatching { | ||
| manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) | ||
| }.getOrNull() | ||
| ?: runCatching { | ||
| manager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER) | ||
| }.getOrNull() | ||
|
|
||
| if (lastKnown != null) { | ||
| send(lastKnown) | ||
| } | ||
|
|
||
| val stream = if (appContext.hasFineLocationPermission() && | ||
| priority == LocationPriority.HIGH_ACCURACY | ||
| ) { | ||
| merge( | ||
| manager.asLocationSource().locationEvents( | ||
| intervalMs = intervalMs, | ||
| minUpdateDistanceM = minUpdateDistanceM, | ||
| priority = LocationPriority.HIGH_ACCURACY, | ||
| ).catch { if (it !is SecurityException) throw it }, | ||
| manager.coarseLocationEvents( | ||
| minTimeMs = intervalMs, | ||
| minDistanceM = minUpdateDistanceM, | ||
| ).catch { if (it !is SecurityException) throw it }, | ||
| ) | ||
| } else { | ||
| manager.coarseLocationEvents( | ||
| minTimeMs = intervalMs, | ||
| minDistanceM = minUpdateDistanceM, | ||
| ).catch { if (it !is SecurityException) throw it } | ||
| } | ||
|
|
||
| stream.collect { send(it) } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val openAppSettings: () -> Unit = remember(appContext) { | ||
| { | ||
| val intent = Intent( | ||
| Settings.ACTION_APPLICATION_DETAILS_SETTINGS, | ||
| Uri.fromParts("package", appContext.packageName, null), | ||
| ).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
| appContext.startActivity(intent) | ||
| } | ||
| } | ||
|
|
||
| var rationaleBeforeRequest by remember { mutableStateOf(false) } | ||
|
|
||
| val permissionLauncher = rememberLauncherForActivityResult( | ||
| contract = ActivityResultContracts.RequestMultiplePermissions(), | ||
| ) { permissions -> | ||
| val granted = permissions.values.any { it } || appContext.hasAnyLocationPermission() | ||
| hasPermission = granted | ||
| if (!granted) { | ||
| val rationaleAfterRequest = activity?.shouldShowLocationRationale() ?: false | ||
| // If shouldShowRequestPermissionRationale was false before the request and remains | ||
| // false after the request was denied, Android suppressed the permission dialog | ||
| // because the permission is permanently denied ("Don't ask again" / USER_FIXED). | ||
| if (!rationaleBeforeRequest && !rationaleAfterRequest) { | ||
| openAppSettings() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val requestPermission: () -> Unit = remember(activity, permissionLauncher, openAppSettings) { | ||
| { | ||
| if (activity == null) { | ||
| openAppSettings() | ||
| } else { | ||
| rationaleBeforeRequest = activity.shouldShowLocationRationale() | ||
| permissionLauncher.launch(LOCATION_PERMISSIONS) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return remember(locationSource, hasPermission, requestPermission, openAppSettings) { | ||
| PlatformLocationController( | ||
| locationSource = locationSource, | ||
| hasPermission = hasPermission, | ||
| requestPermission = requestPermission, | ||
| openAppSettings = openAppSettings, | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the trail grows without limit while the demo is open. For a demo it is probably fine, but should we cap it (e.g. last 500 points) to avoid an ever growing polyline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done—added
MAX_TRAIL_POINTS = 500inDemos.ktto cap thetraillist at the most recent 500 points.