|
| 1 | +package com.google.maps.android.compose |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import android.view.LayoutInflater |
| 5 | +import android.view.View |
| 6 | +import android.view.ViewGroup |
| 7 | +import androidx.compose.material.MaterialTheme |
| 8 | +import androidx.compose.material.Text |
| 9 | +import androidx.compose.runtime.Composable |
| 10 | +import androidx.compose.ui.geometry.Offset // For MarkerComposable anchor |
| 11 | +import androidx.compose.ui.platform.ComposeView |
| 12 | +import androidx.compose.ui.platform.ViewCompositionStrategy |
| 13 | +import androidx.fragment.app.Fragment |
| 14 | +import com.google.android.gms.maps.model.CameraPosition |
| 15 | +import com.google.android.gms.maps.model.LatLng |
| 16 | + |
| 17 | + |
| 18 | +enum class MarkerType { |
| 19 | + CUSTOM_CONTENT_MARKER, // To use com.google.maps.android.compose.MarkerComposable with your Text |
| 20 | + STANDARD_MARKER_WITH_SNIPPET // To use the standard com.google.maps.android.compose.Marker |
| 21 | +} |
| 22 | + |
| 23 | +data class MapConfig( |
| 24 | + val initialLatLng: LatLng, |
| 25 | + val initialZoom: Float, |
| 26 | + val title: String, |
| 27 | + val markerType: MarkerType = MarkerType.CUSTOM_CONTENT_MARKER, // Default |
| 28 | + val standardMarkerSnippet: String? = null |
| 29 | +) |
| 30 | + |
| 31 | +class GoogleMapComposeFragment : Fragment() { |
| 32 | + |
| 33 | + private var mapConfig: MapConfig? = null |
| 34 | + |
| 35 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 36 | + super.onCreate(savedInstanceState) |
| 37 | + arguments?.let { |
| 38 | + val lat = it.getDouble(ARG_LAT, Double.NaN) |
| 39 | + val lng = it.getDouble(ARG_LNG, Double.NaN) |
| 40 | + val zoom = it.getFloat(ARG_ZOOM, 10f) |
| 41 | + val title = it.getString(ARG_TITLE, "Map") |
| 42 | + |
| 43 | + val markerTypeName = it.getString(ARG_MARKER_TYPE) |
| 44 | + val markerType = markerTypeName?.let { name -> |
| 45 | + try { |
| 46 | + MarkerType.valueOf(name) |
| 47 | + } catch (e: IllegalArgumentException) { |
| 48 | + MarkerType.CUSTOM_CONTENT_MARKER |
| 49 | + } |
| 50 | + } ?: MarkerType.CUSTOM_CONTENT_MARKER |
| 51 | + |
| 52 | + val snippet = it.getString(ARG_STANDARD_MARKER_SNIPPET) |
| 53 | + |
| 54 | + if (!lat.isNaN() && !lng.isNaN()) { |
| 55 | + mapConfig = MapConfig( |
| 56 | + initialLatLng = LatLng(lat, lng), |
| 57 | + initialZoom = zoom, |
| 58 | + title = title, |
| 59 | + markerType = markerType, |
| 60 | + standardMarkerSnippet = snippet |
| 61 | + ) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + override fun onCreateView( |
| 67 | + inflater: LayoutInflater, |
| 68 | + container: ViewGroup?, |
| 69 | + savedInstanceState: Bundle? |
| 70 | + ): View = ComposeView(requireContext()).apply { |
| 71 | + setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) |
| 72 | + setContent { |
| 73 | + MaterialTheme { |
| 74 | + val currentConfig = mapConfig ?: MapConfig( |
| 75 | + initialLatLng = LatLng(0.0, 0.0), |
| 76 | + initialZoom = 2f, |
| 77 | + title = "Default Map", |
| 78 | + markerType = MarkerType.CUSTOM_CONTENT_MARKER |
| 79 | + ) |
| 80 | + MapContent(config = currentConfig) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Composable |
| 86 | + fun MapContent(config: MapConfig) { |
| 87 | + val cameraPositionState = rememberCameraPositionState { |
| 88 | + position = CameraPosition.fromLatLngZoom(config.initialLatLng, config.initialZoom) |
| 89 | + } |
| 90 | + |
| 91 | + GoogleMap( |
| 92 | + cameraPositionState = cameraPositionState |
| 93 | + ) { |
| 94 | + when (config.markerType) { |
| 95 | + MarkerType.CUSTOM_CONTENT_MARKER -> { |
| 96 | + val markerState = rememberUpdatedMarkerState(position = config.initialLatLng) |
| 97 | + markerState.position = config.initialLatLng |
| 98 | + |
| 99 | + MarkerComposable( |
| 100 | + state = markerState, |
| 101 | + anchor = Offset(0.5f, 1.0f) |
| 102 | + ){ |
| 103 | + Text(text = "Hello, World! (from ${config.title})") |
| 104 | + } |
| 105 | + } |
| 106 | + MarkerType.STANDARD_MARKER_WITH_SNIPPET -> { |
| 107 | + val markerState = rememberUpdatedMarkerState(position = config.initialLatLng) |
| 108 | + markerState.position = config.initialLatLng |
| 109 | + |
| 110 | + Marker( |
| 111 | + state = markerState, |
| 112 | + title = config.title, |
| 113 | + snippet = config.standardMarkerSnippet ?: "Standard Marker Snippet" // Snippet for the info window |
| 114 | + ) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + companion object { |
| 121 | + private const val ARG_LAT = "arg_lat" |
| 122 | + private const val ARG_LNG = "arg_lng" |
| 123 | + private const val ARG_ZOOM = "arg_zoom" |
| 124 | + private const val ARG_TITLE = "arg_title" |
| 125 | + private const val ARG_MARKER_TYPE = "arg_marker_type" |
| 126 | + private const val ARG_STANDARD_MARKER_SNIPPET = "arg_standard_marker_snippet" |
| 127 | + |
| 128 | + @JvmStatic |
| 129 | + fun newInstance(config: MapConfig): GoogleMapComposeFragment { |
| 130 | + return GoogleMapComposeFragment().apply { |
| 131 | + arguments = Bundle().apply { |
| 132 | + putDouble(ARG_LAT, config.initialLatLng.latitude) |
| 133 | + putDouble(ARG_LNG, config.initialLatLng.longitude) |
| 134 | + putFloat(ARG_ZOOM, config.initialZoom) |
| 135 | + putString(ARG_TITLE, config.title) |
| 136 | + putString(ARG_MARKER_TYPE, config.markerType.name) |
| 137 | + config.standardMarkerSnippet?.let { putString(ARG_STANDARD_MARKER_SNIPPET, it) } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments