Skip to content
Merged
Show file tree
Hide file tree
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 Jun 4, 2026
36c1c1b
feat: migrate iOS multiplatform map implementation to native Google M…
kikoso Jun 4, 2026
73f6bc4
feat: add interactive maps demos to iOS app & upgrade Google Maps SDK…
kikoso Jun 5, 2026
f173701
fix(ios): declare DeveloperSecrets.swift output path & add BUILD_KMP.md
dkhawk Jun 12, 2026
fbae135
feat: consume multiplatform clustering from android-maps-utils KMP br…
kikoso Sep 8, 2026
31d0370
ci: publish KMP maps-utils modules to mavenLocal before building
kikoso Sep 8, 2026
7aed67a
fix: consume KMP maps-utils under its public artifactIds
kikoso Sep 8, 2026
94a8178
build: consume the maps-utils 6.1.0-SNAPSHOT instead of a mavenLocal …
kikoso Oct 2, 2026
57273b2
Merge remote-tracking branch 'origin/main' into feat/experimental-kmp…
kikoso Oct 2, 2026
7c5cd5d
build: publish maps-compose-multiplatform with API checks, lint and i…
kikoso Oct 2, 2026
9087d81
test: run the multiplatform clustering tests on the JVM and iOS, docu…
kikoso Oct 2, 2026
8b43d3e
feat: shared camera state, map content and clustering in maps-compose…
kikoso Oct 2, 2026
425cec8
feat(kmp-demo): add shared multiplatform Location Flow demo
dkhawk Oct 9, 2026
06f7bc4
docs(kmp-demo): add TODO referencing android-maps-utils#1818 in Platf…
dkhawk Oct 9, 2026
c30ec6e
chore: merge feat/experimental-kmp-module into feat/kmp-location-flow…
dkhawk Oct 13, 2026
dfe76f6
chore(kmp-demo): address review feedback on permission handling, mani…
dkhawk Oct 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ maps-utils-core = { module = "com.google.maps.android:android-maps-utils-core",
compose-multiplatform-foundation = { module = "org.jetbrains.compose.foundation:foundation", version.ref = "composeMultiplatform" }
compose-multiplatform-runtime = { module = "org.jetbrains.compose.runtime:runtime", version.ref = "composeMultiplatform" }
compose-multiplatform-ui = { module = "org.jetbrains.compose.ui:ui", version.ref = "composeMultiplatform" }
maps-utils-kmp-core = { module = "com.google.maps.android:android-maps-utils-core", version.ref = "mapsutilsKmp" }
maps-utils-kmp-clustering = { module = "com.google.maps.android:android-maps-utils-clustering", version.ref = "mapsutilsKmp" }
maps-utils-kmp-maps-model = { module = "com.google.maps.android:android-maps-utils-maps-model", version.ref = "mapsutilsKmp" }
maps-utils = { module = "com.google.maps.android:android-maps-utils", version.ref = "mapsutils" }
Expand Down
5 changes: 4 additions & 1 deletion iosApp/create_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
API_KEY="YOUR_API_KEY"

if [ -f "$SECRETS_PATH" ]; then
EXTRACTED_KEY=$(grep -E "^MAPS_API_KEY=" "$SECRETS_PATH" | cut -d'=' -f2 | tr -d '"' | tr -d "'")
EXTRACTED_KEY=$(grep -E "^IOS_MAPS_API_KEY=" "$SECRETS_PATH" | cut -d'=' -f2 | tr -d '"' | tr -d "'")
if [ -z "$EXTRACTED_KEY" ]; then
EXTRACTED_KEY=$(grep -E "^MAPS_API_KEY=" "$SECRETS_PATH" | cut -d'=' -f2 | tr -d '"' | tr -d "'")
fi
if [ ! -z "$EXTRACTED_KEY" ]; then
API_KEY="$EXTRACTED_KEY"
fi
Expand Down
2 changes: 2 additions & 0 deletions iosApp/iosApp/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Demonstrates multiplatform LocationSource Flow on Google Maps.</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchScreen</key>
Expand Down
5 changes: 5 additions & 0 deletions maps-compose-multiplatform-demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,10 @@ kotlin {
implementation(libs.compose.multiplatform.ui)
}
}
androidMain {
dependencies {
implementation(libs.androidx.activity.compose)
}
}
}
}
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>
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,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public val multiplatformDemos: List<MultiplatformDemo> = listOf(
MultiplatformDemo("Map clicks", "Tap to drop a marker, long press to clear them") {
MapClicksDemo()
},
MultiplatformDemo("Location Flow", "Live device GPS streamed via KMP LocationSource Flow") {
LocationFlowDemo()
},
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateListOf
Expand All @@ -47,6 +48,13 @@ import com.google.maps.android.compose.multiplatform.Polyline
import com.google.maps.android.compose.multiplatform.cameraPosition
import com.google.maps.android.compose.multiplatform.rememberCameraPositionState
import com.google.maps.android.compose.multiplatform.rememberMarkerState
import com.google.maps.android.location.Location
import com.google.maps.android.location.LocationPriority
import com.google.maps.android.location.accuracyMeters
import com.google.maps.android.location.latLng
import com.google.maps.android.location.latitude
import com.google.maps.android.location.locationEvents
import com.google.maps.android.location.longitude
import com.google.maps.android.model.LatLng
import com.google.maps.android.model.latitude
import com.google.maps.android.model.longitude
Expand Down Expand Up @@ -272,3 +280,97 @@ internal fun MapClicksDemo() {
private fun Double.format(): String = (round(this * 10_000) / 10_000).toString()

private fun Boolean.onOff(): String = if (this) "on" else "off"


private const val MAX_TRAIL_POINTS = 500

@Composable
internal fun LocationFlowDemo() {
val controller = rememberPlatformLocationController()
val camera = rememberCameraPositionState { position = cameraPosition(SanFrancisco, 14f) }
val markerState = rememberMarkerState(SanFrancisco)
var latestLocation by remember { mutableStateOf<Location?>(null) }
var fixCount by remember { mutableIntStateOf(0) }
var priority by remember { mutableStateOf(LocationPriority.HIGH_ACCURACY) }
val trail = remember { mutableStateListOf<LatLng>() }

LaunchedEffect(controller.locationSource, priority, controller.hasPermission) {
if (!controller.hasPermission) {
latestLocation = null
return@LaunchedEffect
}
controller.locationSource.locationEvents(
intervalMs = 1_000L,
minUpdateDistanceM = 0f,
priority = priority,
).collect { location ->
latestLocation = location
fixCount++
val target = location.latLng
if (trail.lastOrNull() != target) {
if (trail.size >= MAX_TRAIL_POINTS) {
trail.removeAt(0)
}
trail += target
}
Comment on lines +310 to +315

Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Collaborator Author

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 = 500 in Demos.kt to cap the trail list at the most recent 500 points.

markerState.position = target
camera.animate(cameraPosition(target, 15f))
}
}

Box(Modifier.fillMaxSize()) {
GoogleMap(Modifier.fillMaxSize(), cameraPositionState = camera) {
val loc = latestLocation
if (loc != null && controller.hasPermission) {
Marker(
state = markerState,
title = "Live Location (KMP Flow)",
snippet = "Fix #$fixCount (${loc.latitude.format()}, ${loc.longitude.format()})",
)
val accuracyRadius = loc.accuracyMeters.toDouble().coerceAtLeast(25.0)
Circle(
center = loc.latLng,
radius = accuracyRadius,
fillColor = Color(0x441A73E8),
strokeColor = Accent,
strokeWidth = 3f,
)
if (trail.size >= 2) {
Polyline(
points = trail.toList(),
color = Accent,
width = 8f,
)
}
}
}
DemoControls(Modifier.align(Alignment.TopStart)) {
if (!controller.hasPermission) {
DemoLabel(
"Location permission required.\n" +
"Grant permission to stream live LocationSource updates.",
)
Spacer(Modifier.height(8.dp))
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
DemoButton("Grant Permission") { controller.requestPermission() }
DemoButton("App Settings") { controller.openAppSettings() }
}
} else {
val loc = latestLocation
if (loc == null) {
DemoLabel("Waiting for LocationSource.locationEvents()...")
} else {
DemoLabel(
"Flow Fix #$fixCount: ${loc.latitude.format()}, ${loc.longitude.format()}\n" +
"Accuracy: ${loc.accuracyMeters.toDouble().format()}m · Mode: ${priority.name}",
)
}
Spacer(Modifier.height(8.dp))
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
DemoButton("High Accuracy") { priority = LocationPriority.HIGH_ACCURACY }
DemoButton("Balanced") { priority = LocationPriority.BALANCED_POWER_ACCURACY }
}
}
}
}
}
Loading
Loading