Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/lint-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:

- name: Merge SARIF files
run: |
jq -s '{ "$schema": "https://json.schemastore.org/sarif-2.1.0", "version": "2.1.0", "runs": map(.runs) | add }' maps-compose/build/reports/lint-results.sarif maps-compose-utils/build/reports/lint-results.sarif maps-compose-widgets/build/reports/lint-results.sarif app/build/reports/lint-results.sarif > merged.sarif
jq -s '{ "$schema": "https://json.schemastore.org/sarif-2.1.0", "version": "2.1.0", "runs": map(.runs) | add }' maps-compose/build/reports/lint-results.sarif maps-compose-utils/build/reports/lint-results.sarif maps-compose-widgets/build/reports/lint-results.sarif maps-app/build/reports/lint-results.sarif navigation-app/build/reports/lint-results.sarif > merged.sarif

- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v3
Expand Down
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,21 @@ dependencies {

## Sample App

This repository includes a [sample app](app).
Currently, there are two sample apps in the repository: [maps-app](app) and [navigation-app](navigation-app). Each of them run a different version of the Android Maps Compose SDK, where either the maps or the navigation SDK are run, respectively.

To run the demo app, ensure you've met the requirements above then:
To run the maps demo app, ensure you've met the requirements above then:

1. Open the `secrets.properties` file in your top-level directory, and then add the following code. Replace YOUR_API_KEY with your API key. Store your key in this file because secrets.properties is excluded from being checked into a version control system.
If the `secrets.properties` file does not exist, create it in the same folder as the `local.default.properties` file.
```
MAPS_API_KEY=YOUR_API_KEY
```

If you want to use also the Navigation SDK, make sure the PLACES_API_KEY also contains an entry.
```
PLACES_API_KEY=DEFAULT_API_KEY
```

1. Build and run

## Documentation
Expand Down Expand Up @@ -391,6 +397,18 @@ GoogleMap(
}
```

## Using the Navigation SDK

In order to use the Navigation SDK, make sure to include the Composable `NavigationScreen` as follows:

```kotlin
NavigationScreen(
modifier = Modifier.padding(innerPadding),
deviceLocation = location
)
```


</details>

## Maps Compose Utility Library
Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ plugins {
alias(libs.plugins.dokka) apply true
alias(libs.plugins.compose.compiler) apply false
id("com.autonomousapps.dependency-analysis") version "2.0.0"
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false

}

Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
# (not needed, since no requested libraries are pre-AndroidX)
android.enableJetifier=false
#android.enableJetifier=false
# This is needed for the navigation SDK library. It needs the recycler view.
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

Expand Down
41 changes: 34 additions & 7 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
[versions]
accompanistPermissions = "0.37.0"
activitycompose = "1.9.3"
agp = "8.7.2"
androidxtest = "1.6.2"
androidCore = "1.6.1"
androidx-core = "1.15.0"
androidxtest = "1.6.2"
compose-bom = "2024.11.00"
dokka = "1.9.20"
espresso = "3.6.1"
jacoco-plugin = "0.2.1"
junitktx = "1.2.1"
junit = "4.13.2"
junitVersion = "1.2.1"
junitktx = "1.2.1"
kotlin = "2.0.21"
kotlinxCoroutines = "1.9.0"
mapsktx = "5.1.1"
leakcanaryAndroid = "2.12"
lifecycleRuntimeKtx = "2.8.7"
mapsecrets = "2.0.1"
mapsktx = "5.1.1"
navigation = "6.1.0"
org-jacoco-core = "0.8.11"
androidx-core = "1.15.0"
places = "4.1.0"
playServicesLocation = "21.3.0"
robolectric = "4.14.1"
screenshot = "0.0.1-alpha08"
secretsGradlePlugin = "2.0.1"
truth = "1.4.4"

[libraries]
# robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
accompanist-permissions = { module = "com.google.accompanist:accompanist-permissions", version.ref = "accompanistPermissions" }
android-gradle-plugin = { module = "com.android.tools.build:gradle", version.ref = "agp" }
androidx-compose-activity = { module = "androidx.activity:activity-compose", version.ref = "activitycompose" }
androidx-compose-bom = { module = "androidx.compose:compose-bom", version.ref = "compose-bom" }
Expand All @@ -27,25 +39,40 @@ androidx-compose-ui = { module = "androidx.compose.ui:ui" }
androidx-compose-ui-preview-tooling = { module = "androidx.compose.ui:ui-tooling-preview" }
androidx-compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
androidx-core = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "lifecycleRuntimeKtx" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycleRuntimeKtx" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-test-compose-ui = { module = "androidx.compose.ui:ui-test-junit4" }
androidx-test-core = { module = "androidx.test:core", version.ref = "androidCore" }
androidx-test-espresso = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso" }
androidx-test-junit-ktx = { module = "androidx.test.ext:junit-ktx", version.ref = "junitktx" }
androidx-test-rules = { module = "androidx.test:rules", version.ref = "androidCore" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidxtest" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
dokka-plugin = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "dokka" }
jacoco-android-plugin = { module = "com.mxalbert.gradle:jacoco-android", version.ref = "jacoco-plugin", version.require = "0.2.1" }
kotlin = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk7", version.ref = "kotlin" }
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinxCoroutines" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinxCoroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinxCoroutines" }
leakcanary-android = { module = "com.squareup.leakcanary:leakcanary-android", version.ref = "leakcanaryAndroid" }
maps-ktx-std = { module = "com.google.maps.android:maps-ktx", version.ref = "mapsktx" }
maps-ktx-utils = { module = "com.google.maps.android:maps-utils-ktx", version.ref = "mapsktx" }
maps-secrets-plugin = { module = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin", version.ref = "mapsecrets" }
navigation = { module = "com.google.android.libraries.navigation:navigation", version.ref = "navigation" }
org-jacoco-core = { module = "org.jacoco:org.jacoco.core", version.ref = "org-jacoco-core" }
places = { group = "com.google.android.libraries.places", name = "places", version.ref = "places" }
play-services-location = { module = "com.google.android.gms:play-services-location", version.ref = "playServicesLocation" }
test-junit = { module = "junit:junit", version.ref = "junit" }
truth = { module = "com.google.truth:truth", version.ref = "truth" }

[plugins]
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
android-application = { id = "com.android.application", version.ref = "agp" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
screenshot = { id = "com.android.compose.screenshot", version.ref = "screenshot"}
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
screenshot = { id = "com.android.compose.screenshot", version.ref = "screenshot"}
secrets-gradle-plugin = { id = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin", version.ref = "secretsGradlePlugin" }
3 changes: 2 additions & 1 deletion local.defaults.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
MAPS_API_KEY=YOUR_API_KEY
MAPS_API_KEY=YOUR_API_KEY
PLACES_API_KEY=DEFAULT_API_KEY
File renamed without changes.
2 changes: 2 additions & 0 deletions app/build.gradle.kts → maps-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ dependencies {
implementation(libs.kotlinx.coroutines.android)
implementation(libs.androidx.compose.ui.preview.tooling)
debugImplementation(libs.androidx.compose.ui.tooling)
debugImplementation(libs.leakcanary.android)


androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.test.core)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package com.google.maps.android.compose
import android.view.View
import androidx.compose.ui.platform.ComposeView
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.model.Marker

/**
Expand All @@ -36,7 +35,7 @@ import com.google.android.gms.maps.model.Marker
* implementation should be updated.
*/
internal class ComposeInfoWindowAdapter(
private val mapView: MapView,
private val mapViewDelegate: AbstractMapViewDelegate<*>,
private val markerNodeFinder: (Marker) -> MarkerNode?
) : GoogleMap.InfoWindowAdapter {

Expand All @@ -46,10 +45,10 @@ internal class ComposeInfoWindowAdapter(
if (content == null) {
return null
}
val view = ComposeView(mapView.context).apply {
val view = ComposeView(mapViewDelegate.mapView.context).apply {
setContent { content(marker) }
}
mapView.renderComposeViewOnce(view, parentContext = markerNode.compositionContext)
mapViewDelegate.renderComposeViewOnce(view, parentContext = markerNode.compositionContext)
return view
}

Expand All @@ -59,11 +58,10 @@ internal class ComposeInfoWindowAdapter(
if (infoWindow == null) {
return null
}
val view = ComposeView(mapView.context).apply {
val view = ComposeView(mapViewDelegate.mapView.context).apply {
setContent { infoWindow(marker) }
}
mapView.renderComposeViewOnce(view, parentContext = markerNode.compositionContext)
mapViewDelegate.renderComposeViewOnce(view, parentContext = markerNode.compositionContext)
return view
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package com.google.maps.android.compose

import android.content.ComponentCallbacks
import android.content.ComponentCallbacks2
import android.content.Context
import android.content.res.Configuration
import android.location.Location
import android.os.Bundle
Expand All @@ -35,12 +36,15 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.AbstractComposeView
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.findViewTreeLifecycleOwner
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.gms.maps.LocationSource
import com.google.android.gms.maps.MapView
Expand Down Expand Up @@ -98,6 +102,7 @@ public fun GoogleMap(
onPOIClick: ((PointOfInterest) -> Unit)? = null,
contentPadding: PaddingValues = DefaultMapContentPadding,
mapColorScheme: ComposeMapColorScheme? = null,
mapViewCreator: ((Context, GoogleMapOptions) -> AbstractMapViewDelegate<*>)? = null,
content: @Composable @GoogleMapComposable () -> Unit = {},
) {
// When in preview, early return a Box with the received modifier preserving layout
Expand Down Expand Up @@ -145,19 +150,30 @@ public fun GoogleMap(
var subcompositionJob by remember { mutableStateOf<Job?>(null) }
val parentCompositionScope = rememberCoroutineScope()

var delegate by remember {
mutableStateOf<AbstractMapViewDelegate<*>?>(null)
}

AndroidView(
modifier = modifier,
factory = { context ->
MapView(context, googleMapOptionsFactory()).also { mapView ->
if (mapViewCreator != null) {
mapViewCreator(context, googleMapOptionsFactory())
} else {
MapViewDelegate(MapView(context, googleMapOptionsFactory()))
}.also { mapViewDelegate: AbstractMapViewDelegate<*> ->
delegate = mapViewDelegate
val mapView = mapViewDelegate.mapView

val componentCallbacks = object : ComponentCallbacks2 {
override fun onConfigurationChanged(newConfig: Configuration) {}
@Deprecated("Deprecated in Java", ReplaceWith("onTrimMemory(level)"))
override fun onLowMemory() { mapView.onLowMemory() }
override fun onTrimMemory(level: Int) { mapView.onLowMemory() }
override fun onLowMemory() { mapViewDelegate.onLowMemory() }
override fun onTrimMemory(level: Int) { mapViewDelegate.onLowMemory() }
}
context.registerComponentCallbacks(componentCallbacks)

val lifecycleObserver = MapLifecycleEventObserver(mapView)
val lifecycleObserver = MapLifecycleEventObserver(mapViewDelegate)

mapView.tag = MapTagData(componentCallbacks, lifecycleObserver)

Expand All @@ -179,11 +195,11 @@ public fun GoogleMap(
}

mapView.addOnAttachStateChangeListener(onAttachStateListener)
}
}.mapView
},
onReset = { /* View is detached. */ },
onRelease = { mapView ->
val (componentCallbacks, lifecycleObserver) = mapView.tagData
val (componentCallbacks, lifecycleObserver) = delegate!!.tagData
mapView.context.unregisterComponentCallbacks(componentCallbacks)
lifecycleObserver.moveToDestroyedState()
mapView.tag = null
Expand All @@ -193,7 +209,7 @@ public fun GoogleMap(
subcompositionJob = parentCompositionScope.launchSubcomposition(
mapUpdaterState,
parentComposition,
mapView,
delegate!!,
mapClickListeners,
currentContent,
)
Expand All @@ -209,15 +225,15 @@ public fun GoogleMap(
private fun CoroutineScope.launchSubcomposition(
mapUpdaterState: MapUpdaterState,
parentComposition: CompositionContext,
mapView: MapView,
mapViewDelegate: AbstractMapViewDelegate<*>,
mapClickListeners: MapClickListeners,
content: @Composable @GoogleMapComposable () -> Unit,
): Job {
// Use [CoroutineStart.UNDISPATCHED] to kick off GoogleMap loading immediately
return launch(start = CoroutineStart.UNDISPATCHED) {
val map = mapView.awaitMap()
val map = mapViewDelegate.awaitMap()
val composition = Composition(
applier = MapApplier(map, mapView, mapClickListeners),
applier = MapApplier(map, mapViewDelegate, mapClickListeners),
parent = parentComposition
)

Expand Down Expand Up @@ -308,7 +324,65 @@ public fun googleMapFactory(
}
}

private class MapLifecycleEventObserver(private val mapView: MapView) : LifecycleEventObserver {
public interface AbstractMapViewDelegate<T : View> {
public fun onCreate(savedInstanceState: Bundle?)
public fun onStart()
public fun onResume()
public fun onPause()
public fun onStop()
public fun onLowMemory()
public fun onDestroy()
public suspend fun awaitMap(): GoogleMap
public fun renderComposeViewOnce(
view: AbstractComposeView,
parentContext: CompositionContext,
onAddedToWindow: ((View) -> Unit)? = null,
)

public fun startRenderingComposeView(
view: AbstractComposeView,
parentContext: CompositionContext,
): ComposeUiViewRenderer.RenderHandle

public val mapView: T
}

private val <T : View> AbstractMapViewDelegate<T>.tagData: MapTagData
get() = mapView.tag as MapTagData

public class MapViewDelegate(override val mapView: MapView) : AbstractMapViewDelegate<MapView> {
override fun onCreate(savedInstanceState: Bundle?): Unit = mapView.onCreate(savedInstanceState)
override fun onStart(): Unit = mapView.onStart()
override fun onResume(): Unit = mapView.onResume()
override fun onPause(): Unit = mapView.onPause()
override fun onStop(): Unit = mapView.onStop()
override fun onLowMemory(): Unit = mapView.onLowMemory()
override fun onDestroy(): Unit = mapView.onDestroy()
override suspend fun awaitMap(): GoogleMap = mapView.awaitMap()
override fun renderComposeViewOnce(
view: AbstractComposeView,
parentContext: CompositionContext,
onAddedToWindow: ((View) -> Unit)?
) {
mapView.renderComposeViewOnce(
view = view,
parentContext = parentContext,
onAddedToWindow = onAddedToWindow
)
}

override fun startRenderingComposeView(
view: AbstractComposeView,
parentContext: CompositionContext
): ComposeUiViewRenderer.RenderHandle {
return mapView.startRenderingComposeView(
view = view,
parentContext = parentContext,
)
}
}

private class MapLifecycleEventObserver(private val mapView: AbstractMapViewDelegate<*>) : LifecycleEventObserver {
private var currentLifecycleState: Lifecycle.State = Lifecycle.State.INITIALIZED

override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
Expand Down
Loading
Loading