Skip to content
Open
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
12 changes: 12 additions & 0 deletions maps-compose-utils/api/maps-compose-utils.api
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public final class com/google/maps/android/compose/clustering/ComposableSingleto
public final fun getLambda$773099471$maps_compose_utils ()Lkotlin/jvm/functions/Function3;
}

public final class com/google/maps/android/compose/data/DataLayerKt {
public static final fun DataLayerOverlay (Lcom/google/maps/android/data/renderer/model/DataLayer;Lcom/google/maps/android/data/renderer/IconProvider;ZLcom/google/maps/android/collections/MarkerManager;Lcom/google/maps/android/collections/PolygonManager;Lcom/google/maps/android/collections/PolylineManager;Lcom/google/maps/android/collections/GroundOverlayManager;Landroidx/compose/runtime/Composer;II)V
public static final fun DataSceneOverlay (Lcom/google/maps/android/data/renderer/model/DataScene;Lcom/google/maps/android/data/renderer/IconProvider;ZLcom/google/maps/android/collections/MarkerManager;Lcom/google/maps/android/collections/PolygonManager;Lcom/google/maps/android/collections/PolylineManager;Lcom/google/maps/android/collections/GroundOverlayManager;Landroidx/compose/runtime/Composer;II)V
public static final fun rememberDataLayer (ILjava/lang/String;Lkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/google/maps/android/data/renderer/model/DataLayer;
public static final fun rememberDataLayer (Ljava/lang/String;Lkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/google/maps/android/data/renderer/model/DataLayer;
}

public final class com/google/maps/android/compose/heatmaps/HeatmapTileOverlayKt {
public static final fun HeatmapTileOverlay (Ljava/util/Collection;Lcom/google/maps/android/compose/TileOverlayState;ILcom/google/maps/android/heatmaps/Gradient;DDZFZFLkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;III)V
public static final fun WeightedHeatmapTileOverlay (Ljava/util/Collection;Lcom/google/maps/android/compose/TileOverlayState;ILcom/google/maps/android/heatmaps/Gradient;DDZFZFLkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;III)V
}

public final class com/google/maps/android/compose/wms/WmsTileOverlayKt {
public static final fun WmsTileOverlay (Lkotlin/jvm/functions/Function5;Lcom/google/maps/android/compose/TileOverlayState;ZFZFLkotlin/jvm/functions/Function1;IILandroidx/compose/runtime/Composer;II)V
}
Expand Down
2 changes: 2 additions & 0 deletions maps-compose-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ baselineProfile {
// The profile is generated from maps-app. Keep only this library's classes.
filter {
include("com.google.maps.android.compose.clustering.**")
include("com.google.maps.android.compose.data.**")
include("com.google.maps.android.compose.heatmaps.**")
include("com.google.maps.android.compose.wms.**")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
/*
* 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.data

import android.graphics.Bitmap
import androidx.annotation.RawRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalResources
import com.google.android.gms.maps.GoogleMap
import com.google.maps.android.collections.GroundOverlayManager
import com.google.maps.android.collections.MarkerManager
import com.google.maps.android.collections.PolygonManager
import com.google.maps.android.collections.PolylineManager
import com.google.maps.android.compose.GoogleMapComposable
import com.google.maps.android.compose.MapEffect
import com.google.maps.android.compose.MapsComposeExperimentalApi
import com.google.maps.android.compose.rememberReattachClickListenersHandle
import com.google.maps.android.data.DataLayerLoader
import com.google.maps.android.data.renderer.IconProvider
import com.google.maps.android.data.renderer.UrlIconProvider
import com.google.maps.android.data.renderer.mapview.MapViewRenderer
import com.google.maps.android.data.renderer.model.DataLayer
import com.google.maps.android.data.renderer.model.DataScene
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.withContext

/**
* Renders a platform-agnostic [DataScene] onto the map using [MapViewRenderer].
*
* When [scene] changes or the composable leaves composition, previously rendered features are
* removed from the map.
*
* @param scene the [DataScene] to render.
* @param iconProvider the [IconProvider] used to fetch remote marker or ground overlay icons.
* @param useAdvancedMarkers whether to render point features using Advanced Markers.
* @param markerManager an optional [MarkerManager] to manage rendered markers.
* @param polygonManager an optional [PolygonManager] to manage rendered polygons.
* @param polylineManager an optional [PolylineManager] to manage rendered polylines.
* @param groundOverlayManager an optional [GroundOverlayManager] to manage rendered ground overlays.
*/
@Composable
@GoogleMapComposable
@MapsComposeExperimentalApi
public fun DataSceneOverlay(
scene: DataScene,
iconProvider: IconProvider = remember { UrlIconProvider() },
useAdvancedMarkers: Boolean = false,
markerManager: MarkerManager? = null,
polygonManager: PolygonManager? = null,
polylineManager: PolylineManager? = null,
groundOverlayManager: GroundOverlayManager? = null,
) {
val reattach = rememberReattachClickListenersHandle()

MapEffect(
iconProvider,
markerManager,
polygonManager,
polylineManager,
groundOverlayManager,
scene,
useAdvancedMarkers,
) { map ->
val renderer = createAndRenderScene(
map = map,
scene = scene,
iconProvider = iconProvider,
useAdvancedMarkers = useAdvancedMarkers,
markerManager = markerManager,
polygonManager = polygonManager,
polylineManager = polylineManager,
groundOverlayManager = groundOverlayManager,
reattach = reattach,
)
try {
awaitCancellation()
} finally {
renderer.clear()
}
}
}

/**
* Renders a platform-agnostic [DataLayer] (parsed from GeoJSON, KML, KMZ, or GPX) onto the map
* using [MapViewRenderer].
*
* @param layer the [DataLayer] to render.
* @param iconProvider the [IconProvider] used to fetch remote marker or ground overlay icons.
* @param useAdvancedMarkers whether to render point features using Advanced Markers.
* @param markerManager an optional [MarkerManager] to manage rendered markers.
* @param polygonManager an optional [PolygonManager] to manage rendered polygons.
* @param polylineManager an optional [PolylineManager] to manage rendered polylines.
* @param groundOverlayManager an optional [GroundOverlayManager] to manage rendered ground overlays.
*/
@Composable
@GoogleMapComposable
@MapsComposeExperimentalApi
public fun DataLayerOverlay(
layer: DataLayer,
iconProvider: IconProvider = remember { UrlIconProvider() },
useAdvancedMarkers: Boolean = false,
markerManager: MarkerManager? = null,
polygonManager: PolygonManager? = null,
polylineManager: PolylineManager? = null,
groundOverlayManager: GroundOverlayManager? = null,
) {
val scene = remember(layer) { DataScene(listOf(layer)) }
DataSceneOverlay(
scene = scene,
iconProvider = iconProvider,
useAdvancedMarkers = useAdvancedMarkers,
markerManager = markerManager,
polygonManager = polygonManager,
polylineManager = polylineManager,
groundOverlayManager = groundOverlayManager,
)
}

/**
* Asynchronously loads and remembers a [DataLayer] from an application asset file (supporting
* `.geojson`, `.json`, `.kml`, `.kmz`, and `.gpx`) using [DataLayerLoader].
*
* While a new [assetName] is loading, the previously loaded [DataLayer] (if any) is retained to
* prevent visual flickering until parsing completes.
*
* @param assetName the path of the asset file in the application's `assets/` directory.
* @param ioDispatcher the [CoroutineDispatcher] used for reading and parsing the asset file.
* Custom dispatcher instances must be remembered across recompositions.
* @param onError an optional callback invoked with any [Throwable] encountered during loading or parsing.
* @param onLayerLoaded an optional callback invoked with the parsed [DataLayer] (or `null` if
* parsing failed).
* @return the loaded [DataLayer], or `null` while loading or if loading failed and no previous
* layer was loaded.
*/
@Composable
@MapsComposeExperimentalApi
public fun rememberDataLayer(
assetName: String,
ioDispatcher: CoroutineDispatcher = Dispatchers.IO,
onError: ((Throwable) -> Unit)? = null,
onLayerLoaded: (DataLayer?) -> Unit = {},

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: since the return value already gives us the layer and onError the failure, do we need onLayerLoaded at all? Once this ships it will be hard to remove, and I would keep the surface as small as possible.

): DataLayer? {
val context = LocalContext.current
val onLayerLoadedState by rememberUpdatedState(onLayerLoaded)
val onErrorState by rememberUpdatedState(onError)
var loadedLayer by remember { mutableStateOf<DataLayer?>(null) }

LaunchedEffect(context, assetName, ioDispatcher) {
loadLayerWithCallbacks(
defaultErrorMessage = "Failed to load DataLayer from asset: $assetName",
onLayerLoaded = onLayerLoadedState,
onError = onErrorState,
Comment on lines +174 to +175

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.

Since onLayerLoadedState and onErrorState are delegated properties, they are read once here when the effect starts, so a slow parse would still call the old lambdas and rememberUpdatedState is not helping much. Shouldn't we wrap them? Same applies to the raw resource overload.

Suggested change
onLayerLoaded = onLayerLoadedState,
onError = onErrorState,
onLayerLoaded = { onLayerLoadedState(it) },
onError = { onErrorState?.invoke(it) },

) {
DataLayerLoader(context, ioDispatcher).loadAsset(assetName)

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.

I may be missing something, but DataLayerLoader.loadAsset catches every exception and returns null, so onError will almost always receive the generic IllegalStateException here. That was the main reason of exposing the error. Should we add a throwing variant in maps-utils, or at least mention it in the KDoc?

}?.let { loadedLayer = it }
}

return loadedLayer
}

/**
* Asynchronously loads and remembers a [DataLayer] from a raw Android resource (supporting
* GeoJSON, KML, KMZ, and GPX) using [DataLayerLoader].
*
* While a new [rawResourceId] is loading, the previously loaded [DataLayer] (if any) is retained to
* prevent visual flickering until parsing completes.
*
* @param rawResourceId the `@RawRes` resource identifier to load.
* @param fileName an optional file name hint (e.g., `"boroughs.geojson"` or `"campus.kmz"`) used by
* [DataLayerLoader] for extension matching before falling back to content sniffing.
* @param ioDispatcher the [CoroutineDispatcher] used for reading and parsing the raw resource.
* Custom dispatcher instances must be remembered across recompositions.
* @param onError an optional callback invoked with any [Throwable] encountered during loading or parsing.
* @param onLayerLoaded an optional callback invoked with the parsed [DataLayer] (or `null` if
* parsing failed).
* @return the loaded [DataLayer], or `null` while loading or if loading failed and no previous
* layer was loaded.
*/
@Composable
@MapsComposeExperimentalApi
public fun rememberDataLayer(
@RawRes rawResourceId: Int,
fileName: String? = null,
ioDispatcher: CoroutineDispatcher = Dispatchers.IO,
onError: ((Throwable) -> Unit)? = null,
onLayerLoaded: (DataLayer?) -> Unit = {},
): DataLayer? {
val context = LocalContext.current
val resources = LocalResources.current
val onLayerLoadedState by rememberUpdatedState(onLayerLoaded)
val onErrorState by rememberUpdatedState(onError)
var loadedLayer by remember { mutableStateOf<DataLayer?>(null) }

LaunchedEffect(context, resources, rawResourceId, fileName, ioDispatcher) {
loadLayerWithCallbacks(
defaultErrorMessage = "Failed to load DataLayer from raw resource: $rawResourceId",
onLayerLoaded = onLayerLoadedState,
onError = onErrorState,
) {
withContext(ioDispatcher) {
resources.openRawResource(rawResourceId).use { stream ->
DataLayerLoader(context, ioDispatcher).loadInputStream(stream, fileName)
}
}
}?.let { loadedLayer = it }
}

return loadedLayer
}

internal suspend fun loadLayerWithCallbacks(
defaultErrorMessage: String,
onLayerLoaded: (DataLayer?) -> Unit,
onError: ((Throwable) -> Unit)?,
loaderBlock: suspend () -> DataLayer?,
): DataLayer? {
val result = runCatching { loaderBlock() }

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.

runCatching also catches the CancellationException, so if assetName changes mid-load (or the composable leaves the composition) we end up calling onError and onLayerLoaded(null) for a perfectly normal cancellation. Could we rethrow it before handling the failure, and add a test that cancels mid-load?

Suggested change
val result = runCatching { loaderBlock() }
val result = runCatching { loaderBlock() }
result.exceptionOrNull()?.let { if (it is CancellationException) throw it }

val layer = result.getOrNull()
return if (layer != null) {
onLayerLoaded(layer)
layer
} else {
val error = result.exceptionOrNull() ?: IllegalStateException(defaultErrorMessage)
onError?.invoke(error)
onLayerLoaded(null)
null
}
}

internal fun createAndRenderScene(
map: GoogleMap,
scene: DataScene,
iconProvider: IconProvider,
useAdvancedMarkers: Boolean,
markerManager: MarkerManager?,
polygonManager: PolygonManager?,
polylineManager: PolylineManager?,
groundOverlayManager: GroundOverlayManager?,
reattach: () -> Unit,
rendererFactory: (GoogleMap) -> MapViewRenderer = { targetMap ->
MapViewRenderer(
map = targetMap,
iconProvider = iconProvider,
markerManager = markerManager,
polygonManager = polygonManager,
polylineManager = polylineManager,
groundOverlayManager = groundOverlayManager,
)
},
): MapViewRenderer {
val renderer = rendererFactory(map).apply {
this.useAdvancedMarkers = useAdvancedMarkers
}
renderSceneWithCachedImages(renderer, scene)
if (
markerManager != null ||
polygonManager != null ||
polylineManager != null ||
groundOverlayManager != null
) {
reattach()
}
return renderer
}

internal fun renderSceneWithCachedImages(renderer: MapViewRenderer, scene: DataScene) {
scene.layers.forEach { layer ->
val images = layer.properties["images"] as? Map<*, *>
images?.forEach { (key, value) ->
if (key is String && value is Bitmap) {
renderer.cacheImageData(key, value)
}
}
}
renderer.render(scene)
}
Loading
Loading