Skip to content

Commit

Permalink
refactor: sample activity titles and migrate README to advanced markers
Browse files Browse the repository at this point in the history
  • Loading branch information
wangela committed Oct 28, 2023
1 parent 6e2cde7 commit 2b438b3
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 59 deletions.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,33 +158,42 @@ composable elements to the content of the `GoogleMap`.

```kotlin
GoogleMap(
//...
googleMapOptionsFactory = {
GoogleMapOptions().mapId("DEMO_MAP_ID")
},
//...
) {
Marker(
AdvancedMarker(
state = MarkerState(position = LatLng(-34, 151)),
title = "Marker in Sydney"
)
Marker(
AdvancedMarker(
state = MarkerState(position = LatLng(35.66, 139.6)),
title = "Marker in Tokyo"
)
}
```

You can also customize the marker you want to add by using `MarkerComposable`.
You can customize a marker by using `PinConfig` with an `AdvancedMarker`.

```kotlin
val state = MyState()

GoogleMap(
//...
googleMapOptionsFactory = {
GoogleMapOptions().mapId("DEMO_MAP_ID")
},
//...
) {
MarkerComposable(
keys = arrayOf(state),
val pinConfig = PinConfig.builder()
.setBackgroundColor(Color.MAGENTA)
.build()

AdvancedMarker(
state = MarkerState(position = LatLng(-34, 151)),
) {
MyCustomMarker(state)
}
title = "Magenta marker in Sydney",
pinConfig = pinConfig
)
}
```
As this Composable is backed by a rendering of your Composable into a Bitmap, it will not render
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@
<activity
android:name=".BasicMapActivity"
android:exported="false" />
<activity
android:name=".AdvancedMarkersActivity"
android:exported="false"/>
<activity
android:name=".MapInColumnActivity"
android:exported="false"/>
<activity
android:name=".MapClusteringActivity"
android:name=".MarkerClusteringActivity"
android:exported="false"/>
<activity
android:name=".LocationTrackingActivity"
Expand All @@ -60,9 +63,6 @@
<activity
android:name=".CustomControlsActivity"
android:exported="false"/>
<activity
android:name=".AdvancedMarkersActivity"
android:exported="false"/>

<!-- Used by createComponentActivity() for unit testing -->
<activity android:name="androidx.activity.ComponentActivity" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package com.google.maps.android.compose


import android.R
import android.R.drawable.ic_menu_myplaces
import android.graphics.Color
import android.os.Bundle
import android.util.Log
Expand All @@ -32,12 +32,20 @@ import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.gms.maps.MapsInitializer
import com.google.android.gms.maps.OnMapsSdkInitializedCallback
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.PinConfig


private const val TAG = "AdvancedMarkersActivity"

private val santiago = LatLng(-33.4489, -70.6693)
private val bogota = LatLng(-4.7110, -74.0721)
private val lima = LatLng(-12.0464, -77.0428)
private val salvador = LatLng(-12.9777, -38.5016)
private val center = LatLng(-18.000, -58.000)
private val defaultCameraPosition1 = CameraPosition.fromLatLngZoom(center, 2f)
class AdvancedMarkersActivity : ComponentActivity(), OnMapsSdkInitializedCallback {

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -46,15 +54,15 @@ class AdvancedMarkersActivity : ComponentActivity(), OnMapsSdkInitializedCallbac
setContent {
// Observing and controlling the camera's state can be done with a CameraPositionState
val cameraPositionState = rememberCameraPositionState {
position = defaultCameraPosition
position = defaultCameraPosition1
}
val mapProperties by remember {
mutableStateOf(MapProperties(mapType = MapType.NORMAL))
}
val singaporeState = rememberMarkerState(position = singapore)
val singapore2State = rememberMarkerState(position = singapore2)
val singapore3State = rememberMarkerState(position = singapore3)
val singapore4State = rememberMarkerState(position = singapore4)
val marker1State = rememberMarkerState(position = santiago)
val marker2State = rememberMarkerState(position = bogota)
val marker3State = rememberMarkerState(position = lima)
val marker4State = rememberMarkerState(position = salvador)

// Drawing on the map is accomplished with a child-based API
val markerClick: (Marker) -> Boolean = {
Expand All @@ -68,7 +76,7 @@ class AdvancedMarkersActivity : ComponentActivity(), OnMapsSdkInitializedCallbac
GoogleMap(
modifier = Modifier.matchParentSize(),
googleMapOptionsFactory = {
GoogleMapOptions().mapId("45a7dec634a854b0")
GoogleMapOptions().mapId("DEMO_MAP_ID")
},
cameraPositionState = cameraPositionState,
properties = mapProperties,
Expand All @@ -83,48 +91,51 @@ class AdvancedMarkersActivity : ComponentActivity(), OnMapsSdkInitializedCallbac
textView.setTextColor(Color.YELLOW)

AdvancedMarker(
state = singapore3State,
state = marker4State,
onClick = markerClick,
collisionBehavior = 1,
iconView = textView
iconView = textView,
title="Marker 4"
)

val pinConfigBuilder = PinConfig.builder()
pinConfigBuilder.setBackgroundColor(Color.MAGENTA)
pinConfigBuilder.setBorderColor(resources.getColor(R.color.holo_orange_dark))

val pinConfig = pinConfigBuilder.build()
val pinConfig = PinConfig.builder()
.setBackgroundColor(Color.MAGENTA)
.setBorderColor(Color.WHITE)
.build()

AdvancedMarker(
state = singapore2State,
state = marker1State,
onClick = markerClick,
collisionBehavior = 1,
pinConfig = pinConfig
pinConfig = pinConfig,
title="Marker 1"
)
val pinConfigBuilder2 = PinConfig.builder()
val glyphOne = PinConfig.Glyph("A", resources.getColor(R.color.black))
pinConfigBuilder2.setGlyph(glyphOne)

val pinConfig2 = pinConfigBuilder2.build()
val glyphOne = PinConfig.Glyph("A", Color.BLACK)
val pinConfig2 = PinConfig.builder()
.setGlyph(glyphOne)
.build()

AdvancedMarker(
state = singaporeState,
state = marker2State,
onClick = markerClick,
collisionBehavior = 1,
pinConfig = pinConfig2
pinConfig = pinConfig2,
title="Marker 2"
)

val pinConfigBuilder3 = PinConfig.builder()
val glyphImage: Int = R.drawable.ic_menu_report_image
val glyphImage: Int = ic_menu_myplaces
val descriptor = BitmapDescriptorFactory.fromResource(glyphImage)
pinConfigBuilder3.setGlyph(PinConfig.Glyph(descriptor))
val pinConfig3 = pinConfigBuilder3.build()
val pinConfig3 = PinConfig.builder()
.setGlyph(PinConfig.Glyph(descriptor))
.build()

AdvancedMarker(
state = singapore4State,
state = marker3State,
onClick = markerClick,
collisionBehavior = 1,
pinConfig = pinConfig3
pinConfig = pinConfig3,
title="Marker 3"
)

}
Expand Down
22 changes: 11 additions & 11 deletions app/src/main/java/com/google/maps/android/compose/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,35 @@ class MainActivity : ComponentActivity() {
Text(getString(R.string.basic_map_activity))
}
Spacer(modifier = Modifier.padding(5.dp))
Button(
onClick = {
context.startActivity(Intent(context, AdvancedMarkersActivity::class.java))
}) {
Text(getString(R.string.advanced_markers))
}
Spacer(modifier = Modifier.padding(5.dp))
Button(
onClick = {
context.startActivity(
Intent(
context,
MapInColumnActivity::class.java
MarkerClusteringActivity::class.java
)
)
}) {
Text(getString(R.string.map_in_column_activity))
Text(getString(R.string.marker_clustering_activity))
}
Spacer(modifier = Modifier.padding(5.dp))
Button(
onClick = {
context.startActivity(
Intent(
context,
MapClusteringActivity::class.java
MapInColumnActivity::class.java
)
)
}) {
Text(getString(R.string.map_clustering_activity))
Text(getString(R.string.map_in_column_activity))
}
Spacer(modifier = Modifier.padding(5.dp))
Button(
Expand Down Expand Up @@ -117,13 +124,6 @@ class MainActivity : ComponentActivity() {
}) {
Text(getString(R.string.custom_location_button))
}
Spacer(modifier = Modifier.padding(5.dp))
Button(
onClick = {
context.startActivity(Intent(context, AdvancedMarkersActivity::class.java))
}) {
Text(getString(R.string.advanced_markers))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import com.google.maps.android.clustering.ClusterItem
import com.google.maps.android.compose.clustering.Clustering
import kotlin.random.Random

private val TAG = MapClusteringActivity::class.simpleName
private val TAG = MarkerClusteringActivity::class.simpleName

class MapClusteringActivity : ComponentActivity() {
class MarkerClusteringActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand Down Expand Up @@ -60,7 +60,7 @@ fun GoogleMapClustering(items: List<MyItem>) {
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(singapore, 10f)
position = CameraPosition.fromLatLngZoom(singapore, 6f)
}
) {
Clustering(
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
<string name="app_name">android-maps-compose</string>
<string name="main_activity_title">"Maps Compose Demos \uD83D\uDDFA"</string>
<string name="basic_map_activity">Basic Map</string>
<string name="advanced_markers">Advanced Markers</string>
<string name="map_in_column_activity">Map In Column</string>
<string name="map_clustering_activity">Map Clustering</string>
<string name="marker_clustering_activity">Marker Clustering</string>
<string name="location_tracking_activity">Location Tracking</string>
<string name="scale_bar_activity">Scale Bar</string>
<string name="street_view">Street View</string>

<string name="custom_location_button">Custom Location Button</string>
<string name="advanced_markers">Advanced Markers</string>
</resources>

0 comments on commit 2b438b3

Please sign in to comment.