Skip to content

Commit e6cf1b0

Browse files
authored
feat: Add missing Kotlin samples (#428)
* Gradle bump and rename CloseInfoWindowDemoActivity * Adding CameraClampingDemoActivity. * Fix build error. * Add location source.
1 parent ab6e0e8 commit e6cf1b0

15 files changed

+759
-254
lines changed

ApiDemos/kotlin/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ dependencies {
6767
implementation 'androidx.multidex:multidex:2.0.1'
6868

6969
// GMS
70+
gmsImplementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
7071
gmsImplementation 'com.google.android.gms:play-services-maps:17.0.0'
72+
gmsImplementation 'com.google.maps.android:maps-ktx:2.2.0'
7173

7274
// V3
7375
v3Implementation 'com.google.android.libraries.maps:maps:3.1.0-beta'

ApiDemos/kotlin/app/src/gms/AndroidManifest.xml

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,49 @@
1616
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1717
package="com.example.kotlindemos">
1818

19-
<application
20-
android:allowBackup="true"
21-
android:icon="@mipmap/ic_launcher"
22-
android:label="@string/app_name"
23-
android:roundIcon="@mipmap/ic_launcher_round"
24-
android:supportsRtl="true"
25-
android:theme="@style/AppTheme">
19+
<application
20+
android:allowBackup="true"
21+
android:icon="@mipmap/ic_launcher"
22+
android:label="@string/app_name"
23+
android:roundIcon="@mipmap/ic_launcher_round"
24+
android:supportsRtl="true"
25+
android:theme="@style/AppTheme">
2626

27-
<activity android:name=".MainActivity">
28-
<intent-filter>
29-
<action android:name="android.intent.action.MAIN" />
27+
<activity android:name=".MainActivity">
28+
<intent-filter>
29+
<action android:name="android.intent.action.MAIN" />
3030

31-
<category android:name="android.intent.category.LAUNCHER" />
32-
</intent-filter>
33-
</activity>
34-
<activity android:name=".BasicMapDemoActivity" />
35-
<activity android:name=".CameraDemoActivity"/>
36-
<activity android:name=".CircleDemoActivity" />
37-
<activity android:name=".CloseInfoWindowDemoActivity" />
38-
<activity android:name=".EventsDemoActivity" />
39-
<activity android:name=".GroundOverlayDemoActivity" />
40-
<activity android:name=".IndoorDemoActivity" />
41-
<activity android:name=".LayersDemoActivity"/>
42-
<activity android:name=".LiteDemoActivity" />
43-
<activity android:name=".LiteListDemoActivity" />
44-
<activity android:name=".MarkerDemoActivity"/>
45-
<activity android:name=".MyLocationDemoActivity" />
46-
<activity android:name=".PolygonDemoActivity"/>
47-
<activity android:name=".SplitStreetViewPanoramaAndMapDemoActivity" />
48-
<activity android:name=".StreetViewPanoramaBasicDemoActivity" />
49-
<activity android:name=".StreetViewPanoramaEventsDemoActivity" />
50-
<activity android:name=".StreetViewPanoramaNavigationDemoActivity" />
51-
<activity android:name=".StreetViewPanoramaOptionsDemoActivity" />
52-
<activity android:name=".StreetViewPanoramaViewDemoActivity" />
53-
<activity android:name=".TagsDemoActivity"/>
54-
<activity android:name=".TileCoordinateDemoActivity"/>
55-
<activity android:name=".TileOverlayDemoActivity"/>
56-
<activity android:name=".UiSettingsDemoActivity" />
57-
<activity android:name=".VisibleRegionDemoActivity"/>
58-
<activity android:name=".polyline.PolylineDemoActivity" />
59-
<activity android:name=".RawMapViewDemoActivity" />
60-
</application>
31+
<category android:name="android.intent.category.LAUNCHER" />
32+
</intent-filter>
33+
</activity>
34+
<activity android:name=".BasicMapDemoActivity" />
35+
<activity android:name=".CameraDemoActivity" />
36+
<activity android:name=".CameraClampingDemoActivity" />
37+
<activity android:name=".CircleDemoActivity" />
38+
<activity android:name=".MarkerCloseInfoWindowOnRetapDemoActivity" />
39+
<activity android:name=".EventsDemoActivity" />
40+
<activity android:name=".GroundOverlayDemoActivity" />
41+
<activity android:name=".IndoorDemoActivity" />
42+
<activity android:name=".LayersDemoActivity" />
43+
<activity android:name=".LiteDemoActivity" />
44+
<activity android:name=".LiteListDemoActivity" />
45+
<activity android:name=".LocationSourceDemoActivity" />
46+
<activity android:name=".MarkerDemoActivity" />
47+
<activity android:name=".MyLocationDemoActivity" />
48+
<activity android:name=".PolygonDemoActivity" />
49+
<activity android:name=".SplitStreetViewPanoramaAndMapDemoActivity" />
50+
<activity android:name=".StreetViewPanoramaBasicDemoActivity" />
51+
<activity android:name=".StreetViewPanoramaEventsDemoActivity" />
52+
<activity android:name=".StreetViewPanoramaNavigationDemoActivity" />
53+
<activity android:name=".StreetViewPanoramaOptionsDemoActivity" />
54+
<activity android:name=".StreetViewPanoramaViewDemoActivity" />
55+
<activity android:name=".TagsDemoActivity" />
56+
<activity android:name=".TileCoordinateDemoActivity" />
57+
<activity android:name=".TileOverlayDemoActivity" />
58+
<activity android:name=".UiSettingsDemoActivity" />
59+
<activity android:name=".VisibleRegionDemoActivity" />
60+
<activity android:name=".polyline.PolylineDemoActivity" />
61+
<activity android:name=".RawMapViewDemoActivity" />
62+
</application>
6163

6264
</manifest>
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.example.kotlindemos
15+
16+
import android.os.Bundle
17+
import android.util.Log
18+
import android.view.View
19+
import android.widget.Button
20+
import android.widget.TextView
21+
import android.widget.Toast
22+
import androidx.appcompat.app.AppCompatActivity
23+
import androidx.lifecycle.lifecycleScope
24+
import com.google.android.gms.maps.CameraUpdateFactory
25+
import com.google.android.gms.maps.GoogleMap
26+
import com.google.android.gms.maps.GoogleMap.OnCameraIdleListener
27+
import com.google.android.gms.maps.OnMapReadyCallback
28+
import com.google.android.gms.maps.SupportMapFragment
29+
import com.google.android.gms.maps.model.CameraPosition
30+
import com.google.android.gms.maps.model.LatLng
31+
import com.google.android.gms.maps.model.LatLngBounds
32+
import com.google.maps.android.ktx.CameraIdleEvent
33+
import com.google.maps.android.ktx.awaitMap
34+
import com.google.maps.android.ktx.cameraEvents
35+
import kotlinx.coroutines.ExperimentalCoroutinesApi
36+
import kotlinx.coroutines.coroutineScope
37+
import kotlinx.coroutines.flow.collect
38+
import kotlinx.coroutines.launch
39+
40+
/**
41+
* This shows how to constrain the camera to specific boundaries and zoom levels.
42+
*/
43+
class CameraClampingDemoActivity : AppCompatActivity() {
44+
45+
private lateinit var map: GoogleMap
46+
private lateinit var cameraTextView: TextView
47+
private val buttonIdToLatLngBoundsCameraMap = mapOf(
48+
Pair(R.id.clamp_latlng_adelaide, Pair(ADELAIDE, ADELAIDE_CAMERA)),
49+
Pair(R.id.clamp_latlng_pacific, Pair(PACIFIC, PACIFIC_CAMERA)),
50+
)
51+
52+
/**
53+
* Internal min zoom level that can be toggled via the demo.
54+
*/
55+
private var minZoom = DEFAULT_MIN_ZOOM
56+
57+
/**
58+
* Internal max zoom level that can be toggled via the demo.
59+
*/
60+
private var maxZoom = DEFAULT_MAX_ZOOM
61+
62+
@ExperimentalCoroutinesApi
63+
override fun onCreate(savedInstanceState: Bundle?) {
64+
super.onCreate(savedInstanceState)
65+
setContentView(R.layout.camera_clamping_demo)
66+
cameraTextView = findViewById(R.id.camera_text)
67+
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
68+
lifecycleScope.launchWhenCreated {
69+
map = mapFragment.awaitMap()
70+
launch {
71+
map.cameraEvents().collect { event ->
72+
when (event) {
73+
is CameraIdleEvent -> onCameraIdle()
74+
else -> Log.d(TAG, "Got event: $event")
75+
}
76+
}
77+
}
78+
setButtonClickListeners()
79+
}
80+
}
81+
82+
private fun setButtonClickListeners() {
83+
// Min/max zooms
84+
findViewById<Button>(R.id.clamp_min_zoom).setOnClickListener {
85+
minZoom += ZOOM_DELTA
86+
// Constrains the minimum zoom level.
87+
map.setMinZoomPreference(minZoom)
88+
toast("Min zoom preference set to: $minZoom")
89+
}
90+
findViewById<Button>(R.id.clamp_max_zoom).setOnClickListener {
91+
maxZoom -= ZOOM_DELTA
92+
// Constrains the maximum zoom level.
93+
map.setMaxZoomPreference(maxZoom)
94+
toast("Max zoom preference set to: $maxZoom")
95+
}
96+
findViewById<Button>(R.id.clamp_zoom_reset).setOnClickListener {
97+
resetMinMaxZoom()
98+
map.resetMinMaxZoomPreference()
99+
toast("Min/Max zoom preferences reset.")
100+
}
101+
102+
// Clamp
103+
val clampListener: (View) -> Unit = { view ->
104+
buttonIdToLatLngBoundsCameraMap[view.id]?.let { (latLngBounds, camera) ->
105+
map.setLatLngBoundsForCameraTarget(latLngBounds)
106+
map.animateCamera(CameraUpdateFactory.newCameraPosition(camera))
107+
}
108+
}
109+
findViewById<Button>(R.id.clamp_latlng_adelaide).setOnClickListener(clampListener)
110+
findViewById<Button>(R.id.clamp_latlng_pacific).setOnClickListener(clampListener)
111+
findViewById<Button>(R.id.clamp_latlng_reset).setOnClickListener {
112+
map.setLatLngBoundsForCameraTarget(null)
113+
toast("LatLngBounds clamp reset.")
114+
}
115+
}
116+
117+
private fun onCameraIdle() {
118+
cameraTextView.text = map.cameraPosition.toString()
119+
}
120+
121+
122+
private fun toast(msg: String) {
123+
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
124+
}
125+
126+
private fun resetMinMaxZoom() {
127+
minZoom = DEFAULT_MIN_ZOOM
128+
maxZoom = DEFAULT_MAX_ZOOM
129+
}
130+
131+
companion object {
132+
private val TAG = CameraClampingDemoActivity::class.java.name
133+
private const val ZOOM_DELTA = 2.0f
134+
private const val DEFAULT_MIN_ZOOM = 2.0f
135+
private const val DEFAULT_MAX_ZOOM = 22.0f
136+
private val ADELAIDE = LatLngBounds(
137+
LatLng(-35.0, 138.58), LatLng(-34.9, 138.61))
138+
private val ADELAIDE_CAMERA = CameraPosition.Builder()
139+
.target(LatLng(-34.92873, 138.59995)).zoom(20.0f).bearing(0f).tilt(0f).build()
140+
private val PACIFIC = LatLngBounds(
141+
LatLng(-15.0, 165.0), LatLng(15.0, -165.0))
142+
private val PACIFIC_CAMERA = CameraPosition.Builder()
143+
.target(LatLng(0.0, (-180).toDouble())).zoom(4.0f).bearing(0f).tilt(0f).build()
144+
}
145+
}

ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/CloseInfoWindowDemoActivity.kt

Lines changed: 0 additions & 124 deletions
This file was deleted.

0 commit comments

Comments
 (0)