Skip to content

Commit 05b53a2

Browse files
authored
feat: Kotlin samples for RetainMapDemoActivity, ProgrammaticDemoActivity, and OptionsDemoActivity (#430)
* Adding sample for MapInPagerDemoActivity * Bump to gradle 6.5 * Adding MultiMapDemoActivity. * feat: Adding OptionsDemoActivity * Adding ProgrammaticDemoActivity. * Add RetainMapDemoActivity.
1 parent 87f37c0 commit 05b53a2

File tree

7 files changed

+188
-1
lines changed

7 files changed

+188
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,15 @@
4343
<activity android:name=".LiteDemoActivity" />
4444
<activity android:name=".LiteListDemoActivity" />
4545
<activity android:name=".LocationSourceDemoActivity" />
46+
<activity android:name=".MapInPagerDemoActivity" />
4647
<activity android:name=".MarkerDemoActivity" />
48+
<activity android:name=".MultiMapDemoActivity" />
4749
<activity android:name=".MyLocationDemoActivity" />
50+
<activity android:name=".OptionsDemoActivity" />
4851
<activity android:name=".PolygonDemoActivity" />
52+
<activity android:name=".ProgrammaticDemoActivity" />
53+
<activity android:name=".RawMapViewDemoActivity" />
54+
<activity android:name=".RetainMapDemoActivity" />
4955
<activity android:name=".SplitStreetViewPanoramaAndMapDemoActivity" />
5056
<activity android:name=".StreetViewPanoramaBasicDemoActivity" />
5157
<activity android:name=".StreetViewPanoramaEventsDemoActivity" />
@@ -58,7 +64,6 @@
5864
<activity android:name=".UiSettingsDemoActivity" />
5965
<activity android:name=".VisibleRegionDemoActivity" />
6066
<activity android:name=".polyline.PolylineDemoActivity" />
61-
<activity android:name=".RawMapViewDemoActivity" />
6267
</application>
6368

6469
</manifest>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ class DemoDetailsList {
7171
DemoDetails(R.string.my_location_demo_label,
7272
R.string.my_location_demo_details,
7373
MyLocationDemoActivity::class.java),
74+
DemoDetails(R.string.options_demo_label,
75+
R.string.options_demo_description,
76+
OptionsDemoActivity::class.java),
7477
DemoDetails(R.string.polygon_demo_label,
7578
R.string.polygon_demo_details,
7679
PolygonDemoActivity::class.java),
7780
DemoDetails(R.string.polyline_demo_label,
7881
R.string.polyline_demo_description,
7982
PolylineDemoActivity::class.java),
83+
DemoDetails(R.string.programmatic_demo_label,
84+
R.string.programmatic_demo_description,
85+
ProgrammaticDemoActivity::class.java),
8086
DemoDetails(R.string.raw_map_view_demo_label,
8187
R.string.raw_map_view_demo_description,
8288
RawMapViewDemoActivity::class.java),
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 androidx.appcompat.app.AppCompatActivity
18+
19+
/**
20+
* An activity that creates a map with some initial options.
21+
*/
22+
class OptionsDemoActivity : AppCompatActivity() {
23+
override fun onCreate(savedInstanceState: Bundle?) {
24+
super.onCreate(savedInstanceState)
25+
setContentView(R.layout.options_demo)
26+
}
27+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.R
17+
import android.os.Bundle
18+
import androidx.appcompat.app.AppCompatActivity
19+
import androidx.lifecycle.lifecycleScope
20+
import com.google.android.gms.maps.SupportMapFragment
21+
import com.google.android.gms.maps.model.LatLng
22+
import com.google.maps.android.ktx.addMarker
23+
import com.google.maps.android.ktx.awaitMap
24+
25+
/**
26+
* Demonstrates how to instantiate a SupportMapFragment programmatically and add a marker to it.
27+
*/
28+
class ProgrammaticDemoActivity : AppCompatActivity() {
29+
override fun onCreate(savedInstanceState: Bundle?) {
30+
super.onCreate(savedInstanceState)
31+
32+
// It isn't possible to set a fragment's id programmatically so we set a tag instead and
33+
// search for it using that.
34+
val mapFragment =
35+
supportFragmentManager.findFragmentByTag(MAP_FRAGMENT_TAG) as SupportMapFragment? ?:
36+
SupportMapFragment.newInstance().also {
37+
// Then we add it using a FragmentTransaction.
38+
val fragmentTransaction = supportFragmentManager.beginTransaction()
39+
fragmentTransaction.add(R.id.content, it, MAP_FRAGMENT_TAG)
40+
fragmentTransaction.commit()
41+
}
42+
43+
lifecycleScope.launchWhenCreated {
44+
val map = mapFragment.awaitMap()
45+
map.addMarker {
46+
position(LatLng(0.0, 0.0))
47+
title("Marker")
48+
}
49+
}
50+
}
51+
52+
companion object {
53+
private const val MAP_FRAGMENT_TAG = "map"
54+
}
55+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 androidx.appcompat.app.AppCompatActivity
18+
import androidx.lifecycle.lifecycleScope
19+
import com.google.android.gms.maps.GoogleMap
20+
import com.google.android.gms.maps.OnMapReadyCallback
21+
import com.google.android.gms.maps.SupportMapFragment
22+
import com.google.android.gms.maps.model.LatLng
23+
import com.google.android.gms.maps.model.MarkerOptions
24+
import com.google.maps.android.ktx.addMarker
25+
import com.google.maps.android.ktx.awaitMap
26+
27+
/**
28+
* This shows how to retain a map across activity restarts (e.g., from screen rotations), which can
29+
* be faster than relying on state serialization.
30+
*/
31+
class RetainMapDemoActivity : AppCompatActivity() {
32+
override fun onCreate(savedInstanceState: Bundle?) {
33+
super.onCreate(savedInstanceState)
34+
setContentView(R.layout.activity_basic_map_demo)
35+
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
36+
if (savedInstanceState == null) {
37+
// First incarnation of this activity.
38+
mapFragment.retainInstance = true
39+
}
40+
lifecycleScope.launchWhenCreated {
41+
val map = mapFragment.awaitMap()
42+
map.addMarker {
43+
position(LatLng(0.0, 0.0))
44+
title("Marker")
45+
}
46+
}
47+
}
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
Copyright (C) 2012 The Android Open Source Project
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
17+
xmlns:map="http://schemas.android.com/apk/res-auto"
18+
android:id="@+id/map"
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"
21+
android:layout_margin="5dp"
22+
class="com.google.android.gms.maps.SupportMapFragment"
23+
map:cameraBearing="112.5"
24+
map:cameraTargetLat="-33.796923"
25+
map:cameraTargetLng="150.922433"
26+
map:cameraTilt="30"
27+
map:cameraZoom="13"
28+
map:mapType="normal"
29+
map:uiCompass="false"
30+
map:uiRotateGestures="true"
31+
map:uiScrollGestures="false"
32+
map:uiTiltGestures="true"
33+
map:uiZoomControls="false"
34+
map:uiZoomGestures="true" />

ApiDemos/kotlin/app/src/main/res/values/strings.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,16 @@
298298
<string name="multi_map_demo_label">Multiple Maps</string>
299299
<string name="multi_map_demo_description">Demonstrates how to show multiple maps in a single activity.</string>
300300

301+
<!-- Options -->
302+
<string name="options_demo_label">Options</string>
303+
<string name="options_demo_description">Demonstrates how to use Options to initialize a map.</string>
304+
305+
<!-- Programmatic -->
306+
<string name="programmatic_demo_label">Programmatically add map</string>
307+
<string name="programmatic_demo_description">Demonstrates how to add a MapFragment programmatically.</string>
308+
309+
<!-- Retain Map -->
310+
<string name="retain_map_demo_label">Retain map</string>
311+
<string name="retain_map_demo_description">Demonstrates how to reuse a MapFragment.</string>
312+
301313
</resources>

0 commit comments

Comments
 (0)