-
Notifications
You must be signed in to change notification settings - Fork 173
chore: added Fragment sample #727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
23 changes: 23 additions & 0 deletions
23
maps-app/src/main/java/com/google/maps/android/compose/FragmentDemoActivity.kt
This file contains hidden or 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,23 @@ | ||
| package com.google.maps.android.compose | ||
|
|
||
|
|
||
| import android.os.Bundle | ||
| import androidx.fragment.app.FragmentActivity | ||
| import androidx.viewpager2.widget.ViewPager2 | ||
|
|
||
| class FragmentDemoActivity : FragmentActivity() { | ||
|
|
||
| private lateinit var viewPager: ViewPager2 | ||
| private lateinit var pagerAdapter: MapFragmentPagerAdapter | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_fragment_demo) | ||
|
|
||
| viewPager = findViewById(R.id.view_pager) | ||
|
|
||
| pagerAdapter = MapFragmentPagerAdapter(this) | ||
| viewPager.adapter = pagerAdapter | ||
|
|
||
| } | ||
| } |
142 changes: 142 additions & 0 deletions
142
maps-app/src/main/java/com/google/maps/android/compose/GoogleMapComposeFragment.kt
This file contains hidden or 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,142 @@ | ||
| package com.google.maps.android.compose | ||
|
|
||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.compose.material.MaterialTheme | ||
| import androidx.compose.material.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.geometry.Offset // For MarkerComposable anchor | ||
| import androidx.compose.ui.platform.ComposeView | ||
| import androidx.compose.ui.platform.ViewCompositionStrategy | ||
| import androidx.fragment.app.Fragment | ||
| import com.google.android.gms.maps.model.CameraPosition | ||
| import com.google.android.gms.maps.model.LatLng | ||
|
|
||
|
|
||
| enum class MarkerType { | ||
| CUSTOM_CONTENT_MARKER, // To use com.google.maps.android.compose.MarkerComposable with your Text | ||
| STANDARD_MARKER_WITH_SNIPPET // To use the standard com.google.maps.android.compose.Marker | ||
| } | ||
|
|
||
| data class MapConfig( | ||
| val initialLatLng: LatLng, | ||
| val initialZoom: Float, | ||
| val title: String, | ||
| val markerType: MarkerType = MarkerType.CUSTOM_CONTENT_MARKER, // Default | ||
| val standardMarkerSnippet: String? = null | ||
| ) | ||
|
|
||
| class GoogleMapComposeFragment : Fragment() { | ||
|
|
||
| private var mapConfig: MapConfig? = null | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| arguments?.let { | ||
| val lat = it.getDouble(ARG_LAT, Double.NaN) | ||
| val lng = it.getDouble(ARG_LNG, Double.NaN) | ||
| val zoom = it.getFloat(ARG_ZOOM, 10f) | ||
| val title = it.getString(ARG_TITLE, "Map") | ||
|
|
||
| val markerTypeName = it.getString(ARG_MARKER_TYPE) | ||
| val markerType = markerTypeName?.let { name -> | ||
| try { | ||
| MarkerType.valueOf(name) | ||
| } catch (e: IllegalArgumentException) { | ||
| MarkerType.CUSTOM_CONTENT_MARKER | ||
| } | ||
| } ?: MarkerType.CUSTOM_CONTENT_MARKER | ||
|
|
||
| val snippet = it.getString(ARG_STANDARD_MARKER_SNIPPET) | ||
|
|
||
| if (!lat.isNaN() && !lng.isNaN()) { | ||
| mapConfig = MapConfig( | ||
| initialLatLng = LatLng(lat, lng), | ||
| initialZoom = zoom, | ||
| title = title, | ||
| markerType = markerType, | ||
| standardMarkerSnippet = snippet | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View = ComposeView(requireContext()).apply { | ||
| setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) | ||
| setContent { | ||
| MaterialTheme { | ||
| val currentConfig = mapConfig ?: MapConfig( | ||
| initialLatLng = LatLng(0.0, 0.0), | ||
| initialZoom = 2f, | ||
| title = "Default Map", | ||
| markerType = MarkerType.CUSTOM_CONTENT_MARKER | ||
| ) | ||
| MapContent(config = currentConfig) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| fun MapContent(config: MapConfig) { | ||
| val cameraPositionState = rememberCameraPositionState { | ||
| position = CameraPosition.fromLatLngZoom(config.initialLatLng, config.initialZoom) | ||
| } | ||
|
|
||
| GoogleMap( | ||
| cameraPositionState = cameraPositionState | ||
| ) { | ||
| when (config.markerType) { | ||
| MarkerType.CUSTOM_CONTENT_MARKER -> { | ||
| val markerState = rememberUpdatedMarkerState(position = config.initialLatLng) | ||
| markerState.position = config.initialLatLng | ||
|
|
||
| MarkerComposable( | ||
| state = markerState, | ||
| anchor = Offset(0.5f, 1.0f) | ||
| ){ | ||
| Text(text = "Hello, World! (from ${config.title})") | ||
| } | ||
| } | ||
| MarkerType.STANDARD_MARKER_WITH_SNIPPET -> { | ||
| val markerState = rememberUpdatedMarkerState(position = config.initialLatLng) | ||
| markerState.position = config.initialLatLng | ||
|
|
||
| Marker( | ||
| state = markerState, | ||
| title = config.title, | ||
| snippet = config.standardMarkerSnippet ?: "Standard Marker Snippet" // Snippet for the info window | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val ARG_LAT = "arg_lat" | ||
| private const val ARG_LNG = "arg_lng" | ||
| private const val ARG_ZOOM = "arg_zoom" | ||
| private const val ARG_TITLE = "arg_title" | ||
| private const val ARG_MARKER_TYPE = "arg_marker_type" | ||
| private const val ARG_STANDARD_MARKER_SNIPPET = "arg_standard_marker_snippet" | ||
|
|
||
| @JvmStatic | ||
| fun newInstance(config: MapConfig): GoogleMapComposeFragment { | ||
| return GoogleMapComposeFragment().apply { | ||
| arguments = Bundle().apply { | ||
| putDouble(ARG_LAT, config.initialLatLng.latitude) | ||
| putDouble(ARG_LNG, config.initialLatLng.longitude) | ||
| putFloat(ARG_ZOOM, config.initialZoom) | ||
| putString(ARG_TITLE, config.title) | ||
| putString(ARG_MARKER_TYPE, config.markerType.name) | ||
| config.standardMarkerSnippet?.let { putString(ARG_STANDARD_MARKER_SNIPPET, it) } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
33 changes: 33 additions & 0 deletions
33
maps-app/src/main/java/com/google/maps/android/compose/MapFragmentPagerAdapter.kt
This file contains hidden or 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,33 @@ | ||
| package com.google.maps.android.compose | ||
|
|
||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.FragmentActivity | ||
| import androidx.viewpager2.adapter.FragmentStateAdapter | ||
| import com.google.android.gms.maps.model.LatLng | ||
|
|
||
| class MapFragmentPagerAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) { | ||
|
|
||
| private val mapConfigs = listOf( | ||
| MapConfig( // First map - Los Angeles | ||
| initialLatLng = LatLng(34.0522, -118.2437), | ||
| initialZoom = 10f, | ||
| title = "Los Angeles", | ||
| // LA gets the custom content marker | ||
| markerType = MarkerType.CUSTOM_CONTENT_MARKER | ||
| ), | ||
| MapConfig( // Second map - New York City | ||
| initialLatLng = LatLng(40.7128, -74.0060), | ||
| initialZoom = 10f, | ||
| title = "New York City", | ||
| // NYC gets the standard marker | ||
| markerType = MarkerType.STANDARD_MARKER_WITH_SNIPPET, | ||
| standardMarkerSnippet = "The Big Apple!" | ||
| ) | ||
| ) | ||
|
|
||
| override fun getItemCount(): Int = mapConfigs.size | ||
|
|
||
| override fun createFragment(position: Int): Fragment { | ||
| return GoogleMapComposeFragment.newInstance(mapConfigs[position]) | ||
| } | ||
| } |
This file contains hidden or 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,31 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:app="http://schemas.android.com/apk/res-auto" | ||
| xmlns:tools="http://schemas.android.com/tools" | ||
| android:id="@+id/container" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| tools:context=".FragmentDemoActivity"> | ||
|
|
||
| <androidx.viewpager2.widget.ViewPager2 | ||
| android:id="@+id/view_pager" | ||
| android:layout_width="0dp" | ||
| android:layout_height="0dp" | ||
| app:layout_constraintBottom_toBottomOf="parent" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toTopOf="parent" /> | ||
|
|
||
| <TextView | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_margin="16dp" | ||
| android:background="#80000000" | ||
| android:padding="16dp" | ||
| android:text="Swipe to switch between views" | ||
| android:textColor="@android:color/white" | ||
| app:layout_constraintBottom_toBottomOf="parent" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" /> | ||
|
|
||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / Android Lint
Hardcoded text Warning