-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Custom info window. Change-Id: Id5cc2609f49faeb440a5eaa0c8f9ab0c69a6f55d * Create ComposeInfoWindowAdapter. Change-Id: Ic984680283e6a5f13e8187959769279fec5cef38 * Creating ComposeInfoWindowComponent Change-Id: Ib3c4564c8f4d0e40dc51db5d45a1587508b2e84f * Documentation. Change-Id: If1101d49c2d77151a845f295741502af0f36a7fe * Add instructions to READM. Change-Id: I55753e939e7e62d1014c4494c37e98b5a969e737 * Using infoContents and infoWindow properties for customizing the info window. Change-Id: Ib1a146a343b5ac4c9006b33226054f16c23299fb * Rename to Change-Id: I5a536a89c9fe0d1616a54943974b89857c1512d4 * Update README.md Co-authored-by: Ben Trengrove <bentrengrove@users.noreply.github.com> * Update README.md * Update ComposeInfoWindowAdapter.kt * Create MarkerInfoWindow and MarkerInfoWindowContent. Change-Id: I3ebe7f2b7deb74278c0187a91ec062dd1cfc7d59 * Update documentation. Change-Id: Ia985a45dc9990647a873682bca20b5e559e3cf26 * Fix build issues. Change-Id: I199905fc53d02fb2d19022ba45867ee92b747efe Co-authored-by: Ben Trengrove <bentrengrove@users.noreply.github.com>
- Loading branch information
1 parent
6fbc9fa
commit 8ef8074
Showing
7 changed files
with
377 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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
69 changes: 69 additions & 0 deletions
69
maps-compose/src/main/java/com/google/maps/android/compose/ComposeInfoWindowAdapter.kt
This file contains 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,69 @@ | ||
package com.google.maps.android.compose | ||
|
||
import android.view.View | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionContext | ||
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 | ||
|
||
/** | ||
* An InfoWindowAdapter that returns a [ComposeView] for drawing a marker's | ||
* info window. | ||
* | ||
* Note: As of version 18.0.2 of the Maps SDK, info windows are drawn by | ||
* creating a bitmap of the [View]s returned in the [GoogleMap.InfoWindowAdapter] | ||
* interface methods. The returned views are never attached to a window, | ||
* instead, they are drawn to a bitmap canvas. This breaks the assumption | ||
* [ComposeView] makes where it must eventually be attached to a window. As a | ||
* workaround, the contained window is temporarily attached to the MapView so | ||
* that the contents of the ComposeViews are rendered. | ||
* | ||
* Eventually when info windows are no longer implemented this way, this | ||
* implementation should be updated. | ||
*/ | ||
internal class ComposeInfoWindowAdapter( | ||
private val mapView: MapView, | ||
private val markerNodeFinder: (Marker) -> MarkerNode? | ||
) : GoogleMap.InfoWindowAdapter { | ||
|
||
private val infoWindowView: ComposeView | ||
get() = ComposeView(mapView.context).apply { | ||
mapView.addView(this) | ||
} | ||
|
||
override fun getInfoContents(marker: Marker): View? { | ||
val markerNode = markerNodeFinder(marker) ?: return null | ||
val content = markerNode.infoContent | ||
if (content == null) { | ||
return null | ||
} | ||
return infoWindowView.applyAndRemove(markerNode.compositionContext) { | ||
content(marker) | ||
} | ||
} | ||
|
||
override fun getInfoWindow(marker: Marker): View? { | ||
val markerNode = markerNodeFinder(marker) ?: return null | ||
val infoWindow = markerNode.infoWindow | ||
if (infoWindow == null) { | ||
return null | ||
} | ||
return infoWindowView.applyAndRemove(markerNode.compositionContext) { | ||
infoWindow(marker) | ||
} | ||
} | ||
|
||
private fun ComposeView.applyAndRemove( | ||
parentContext: CompositionContext, | ||
content: @Composable () -> Unit | ||
): ComposeView { | ||
val result = this.apply { | ||
setParentCompositionContext(parentContext) | ||
setContent(content) | ||
} | ||
(this.parent as? MapView)?.removeView(this) | ||
return result | ||
} | ||
} |
This file contains 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 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 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.