Skip to content

Commit 7f1efcd

Browse files
authored
chore: Add EventsDemoActivity in Kotlin (googlemaps-samples#220)
Also apply following changes to Java version: * Refactor variables to remove Hungarian notation * Remove redundant cast for TextView
1 parent c626549 commit 7f1efcd

File tree

6 files changed

+131
-12
lines changed

6 files changed

+131
-12
lines changed

ApiDemos/java/app/src/gms/java/com/example/mapdemo/EventsDemoActivity.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ public class EventsDemoActivity extends AppCompatActivity
3535
implements OnMapClickListener, OnMapLongClickListener, OnCameraIdleListener,
3636
OnMapReadyCallback {
3737

38-
private TextView mTapTextView;
39-
private TextView mCameraTextView;
40-
private GoogleMap mMap;
38+
private TextView tapTextView;
39+
private TextView cameraTextView;
40+
private GoogleMap map;
4141

4242
@Override
4343
protected void onCreate(Bundle savedInstanceState) {
4444
super.onCreate(savedInstanceState);
4545
setContentView(R.layout.events_demo);
4646

47-
mTapTextView = (TextView) findViewById(R.id.tap_text);
48-
mCameraTextView = (TextView) findViewById(R.id.camera_text);
47+
tapTextView = findViewById(R.id.tap_text);
48+
cameraTextView = findViewById(R.id.camera_text);
4949

5050
SupportMapFragment mapFragment =
5151
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
@@ -54,24 +54,24 @@ protected void onCreate(Bundle savedInstanceState) {
5454

5555
@Override
5656
public void onMapReady(GoogleMap map) {
57-
mMap = map;
58-
mMap.setOnMapClickListener(this);
59-
mMap.setOnMapLongClickListener(this);
60-
mMap.setOnCameraIdleListener(this);
57+
this.map = map;
58+
this.map.setOnMapClickListener(this);
59+
this.map.setOnMapLongClickListener(this);
60+
this.map.setOnCameraIdleListener(this);
6161
}
6262

6363
@Override
6464
public void onMapClick(LatLng point) {
65-
mTapTextView.setText("tapped, point=" + point);
65+
tapTextView.setText("tapped, point=" + point);
6666
}
6767

6868
@Override
6969
public void onMapLongClick(LatLng point) {
70-
mTapTextView.setText("long pressed, point=" + point);
70+
tapTextView.setText("long pressed, point=" + point);
7171
}
7272

7373
@Override
7474
public void onCameraIdle() {
75-
mCameraTextView.setText(mMap.getCameraPosition().toString());
75+
cameraTextView.setText(map.getCameraPosition().toString());
7676
}
7777
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class DemoDetailsList {
3131
DemoDetails(R.string.close_info_window_demo_label,
3232
R.string.close_info_window_demo_details,
3333
CloseInfoWindowDemoActivity::class.java),
34+
DemoDetails(
35+
R.string.events_demo_label,
36+
R.string.events_demo_details,
37+
EventsDemoActivity::class.java),
3438
DemoDetails(R.string.layers_demo_label, R.string.layers_demo_description,
3539
LayersDemoActivity::class.java),
3640
DemoDetails(R.string.lite_demo_label, R.string.lite_demo_details,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.widget.TextView
18+
import androidx.appcompat.app.AppCompatActivity
19+
import com.google.android.gms.maps.GoogleMap
20+
import com.google.android.gms.maps.GoogleMap.*
21+
import com.google.android.gms.maps.OnMapReadyCallback
22+
import com.google.android.gms.maps.SupportMapFragment
23+
import com.google.android.gms.maps.model.LatLng
24+
25+
/**
26+
* This shows how to listen to some [GoogleMap] events.
27+
*/
28+
class EventsDemoActivity : AppCompatActivity(), OnMapClickListener,
29+
OnMapLongClickListener, OnCameraIdleListener, OnMapReadyCallback {
30+
31+
private lateinit var tapTextView: TextView
32+
private lateinit var cameraTextView: TextView
33+
private lateinit var map: GoogleMap
34+
35+
override fun onCreate(savedInstanceState: Bundle?) {
36+
super.onCreate(savedInstanceState)
37+
setContentView(R.layout.events_demo)
38+
tapTextView = findViewById(R.id.tap_text)
39+
cameraTextView = findViewById(R.id.camera_text)
40+
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
41+
mapFragment?.getMapAsync(this)
42+
}
43+
44+
override fun onMapReady(googleMap: GoogleMap?) {
45+
// return early if the map was not initialised properly
46+
map = googleMap ?: return
47+
map.setOnMapClickListener(this)
48+
map.setOnMapLongClickListener(this)
49+
map.setOnCameraIdleListener(this)
50+
}
51+
52+
override fun onMapClick(point: LatLng) {
53+
tapTextView.text = "tapped, point=$point"
54+
}
55+
56+
override fun onMapLongClick(point: LatLng) {
57+
tapTextView.text = "long pressed, point=$point"
58+
}
59+
60+
override fun onCameraIdle() {
61+
if(!::map.isInitialized) return
62+
cameraTextView.text = map.cameraPosition.toString()
63+
}
64+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent">
19+
20+
<fragment
21+
android:id="@+id/map"
22+
android:layout_width="match_parent"
23+
android:layout_height="match_parent"
24+
class="com.google.android.gms.maps.SupportMapFragment" />
25+
26+
<LinearLayout
27+
android:layout_width="match_parent"
28+
android:layout_height="wrap_content"
29+
android:background="@color/white"
30+
android:orientation="vertical">
31+
32+
<TextView
33+
android:id="@+id/tap_text"
34+
android:text="@string/tap_instructions"
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content" />
37+
38+
<TextView
39+
android:id="@+id/camera_text"
40+
android:text="@string/move_the_camera"
41+
android:layout_width="match_parent"
42+
android:layout_height="wrap_content" />
43+
</LinearLayout>
44+
</FrameLayout>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<activity android:name=".StreetViewPanoramaEventsDemoActivity" />
6464
<activity android:name=".StreetViewPanoramaViewDemoActivity" />
6565
<activity android:name=".LiteDemoActivity" />
66+
<activity android:name=".EventsDemoActivity" />
6667
</application>
6768

6869
</manifest>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
<string name="close_info_window_demo_label">Close Info Window Demo</string>
5050
<string name="close_info_window_demo_details">Demonstrates how to close the info window when the currently selected marker is retapped.</string>
5151

52+
<!-- Events Demo -->
53+
<string name="events_demo_label">Events</string>
54+
<string name="events_demo_details">Demonstrates event handling.</string>
55+
<string name="move_the_camera">Move the camera</string>
56+
<string name="tap_instructions">Tap or long press on the map</string>
57+
5258
<!-- Layers Demo -->
5359
<string name="buildings">Buildings</string>
5460
<string name="hybrid">Hybrid</string>

0 commit comments

Comments
 (0)