Skip to content

Commit

Permalink
feat: added support for Advanced Markers
Browse files Browse the repository at this point in the history
  • Loading branch information
kikoso committed Oct 20, 2023
1 parent b4aaaa8 commit 6e2cde7
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<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
@@ -0,0 +1,142 @@
// Copyright 2023 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


import android.R
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
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.Marker
import com.google.android.gms.maps.model.PinConfig


private const val TAG = "AdvancedMarkersActivity"

class AdvancedMarkersActivity : ComponentActivity(), OnMapsSdkInitializedCallback {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MapsInitializer.initialize(applicationContext, MapsInitializer.Renderer.LATEST, this)
setContent {
// Observing and controlling the camera's state can be done with a CameraPositionState
val cameraPositionState = rememberCameraPositionState {
position = defaultCameraPosition
}
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)

// Drawing on the map is accomplished with a child-based API
val markerClick: (Marker) -> Boolean = {
Log.d(TAG, "${it.title} was clicked")
cameraPositionState.projection?.let { projection ->
Log.d(TAG, "The current projection is: $projection")
}
false
}
Box(Modifier.fillMaxSize()) {
GoogleMap(
modifier = Modifier.matchParentSize(),
googleMapOptionsFactory = {
GoogleMapOptions().mapId("45a7dec634a854b0")
},
cameraPositionState = cameraPositionState,
properties = mapProperties,
onPOIClick = {
Log.d(TAG, "POI clicked: ${it.name}")
}
) {

val textView = TextView(this@AdvancedMarkersActivity)
textView.text = "Hello!!"
textView.setBackgroundColor(Color.BLACK)
textView.setTextColor(Color.YELLOW)

AdvancedMarker(
state = singapore3State,
onClick = markerClick,
collisionBehavior = 1,
iconView = textView
)

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

val pinConfig = pinConfigBuilder.build()

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

val pinConfig2 = pinConfigBuilder2.build()

AdvancedMarker(
state = singaporeState,
onClick = markerClick,
collisionBehavior = 1,
pinConfig = pinConfig2
)

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

AdvancedMarker(
state = singapore4State,
onClick = markerClick,
collisionBehavior = 1,
pinConfig = pinConfig3
)

}
}
}
}

override fun onMapsSdkInitialized(renderer: MapsInitializer.Renderer) {
when (renderer) {
MapsInitializer.Renderer.LATEST -> Log.d("MapsDemo", "The latest version of the renderer is used.")
MapsInitializer.Renderer.LEGACY -> Log.d("MapsDemo", "The legacy version of the renderer is used.")
else -> {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.google.maps.android.compose.theme.MapsComposeSampleTheme

private const val TAG = "MapSampleActivity"

class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -119,6 +117,13 @@ 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
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
<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>
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ kotlin = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk7", version.ref = "ko
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
maps-ktx-std = { module = "com.google.maps.android:maps-ktx", version.ref = "mapsktx" }
maps-ktx-utils = { module = "com.google.maps.android:maps-utils-ktx", version.ref = "mapsktx" }
maps-playservice = { module = "com.google.android.gms:play-services-maps", version.require = "18.1.0" }
maps-playservice = { module = "com.google.android.gms:play-services-maps", version.require = "18.2.0" }
maps-secrets-plugin = { module = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin", version.ref = "mapsecrets" }
material = { module = "com.google.android.material:material", version.ref = "material" }
test-junit = { module = "junit:junit", version.ref = "junit" }
Expand Down
Loading

0 comments on commit 6e2cde7

Please sign in to comment.