chore: Add EventsDemoActivity in Kotlin (#220)

Also apply following changes to Java version:
* Refactor variables to remove Hungarian notation
* Remove redundant cast for TextView
This commit is contained in:
Sean Barbeau 2020-05-29 14:15:34 -04:00 committed by GitHub
parent c626549560
commit 7f1efcdab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 131 additions and 12 deletions

View File

@ -35,17 +35,17 @@ public class EventsDemoActivity extends AppCompatActivity
implements OnMapClickListener, OnMapLongClickListener, OnCameraIdleListener,
OnMapReadyCallback {
private TextView mTapTextView;
private TextView mCameraTextView;
private GoogleMap mMap;
private TextView tapTextView;
private TextView cameraTextView;
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.events_demo);
mTapTextView = (TextView) findViewById(R.id.tap_text);
mCameraTextView = (TextView) findViewById(R.id.camera_text);
tapTextView = findViewById(R.id.tap_text);
cameraTextView = findViewById(R.id.camera_text);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
@ -54,24 +54,24 @@ public class EventsDemoActivity extends AppCompatActivity
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
mMap.setOnMapClickListener(this);
mMap.setOnMapLongClickListener(this);
mMap.setOnCameraIdleListener(this);
this.map = map;
this.map.setOnMapClickListener(this);
this.map.setOnMapLongClickListener(this);
this.map.setOnCameraIdleListener(this);
}
@Override
public void onMapClick(LatLng point) {
mTapTextView.setText("tapped, point=" + point);
tapTextView.setText("tapped, point=" + point);
}
@Override
public void onMapLongClick(LatLng point) {
mTapTextView.setText("long pressed, point=" + point);
tapTextView.setText("long pressed, point=" + point);
}
@Override
public void onCameraIdle() {
mCameraTextView.setText(mMap.getCameraPosition().toString());
cameraTextView.setText(map.getCameraPosition().toString());
}
}

View File

@ -31,6 +31,10 @@ class DemoDetailsList {
DemoDetails(R.string.close_info_window_demo_label,
R.string.close_info_window_demo_details,
CloseInfoWindowDemoActivity::class.java),
DemoDetails(
R.string.events_demo_label,
R.string.events_demo_details,
EventsDemoActivity::class.java),
DemoDetails(R.string.layers_demo_label, R.string.layers_demo_description,
LayersDemoActivity::class.java),
DemoDetails(R.string.lite_demo_label, R.string.lite_demo_details,

View File

@ -0,0 +1,64 @@
// Copyright 2020 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.example.kotlindemos
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMap.*
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
/**
* This shows how to listen to some [GoogleMap] events.
*/
class EventsDemoActivity : AppCompatActivity(), OnMapClickListener,
OnMapLongClickListener, OnCameraIdleListener, OnMapReadyCallback {
private lateinit var tapTextView: TextView
private lateinit var cameraTextView: TextView
private lateinit var map: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.events_demo)
tapTextView = findViewById(R.id.tap_text)
cameraTextView = findViewById(R.id.camera_text)
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap?) {
// return early if the map was not initialised properly
map = googleMap ?: return
map.setOnMapClickListener(this)
map.setOnMapLongClickListener(this)
map.setOnCameraIdleListener(this)
}
override fun onMapClick(point: LatLng) {
tapTextView.text = "tapped, point=$point"
}
override fun onMapLongClick(point: LatLng) {
tapTextView.text = "long pressed, point=$point"
}
override fun onCameraIdle() {
if(!::map.isInitialized) return
cameraTextView.text = map.cameraPosition.toString()
}
}

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright (C) 2012 The Android Open Source Project
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.
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<TextView
android:id="@+id/tap_text"
android:text="@string/tap_instructions"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/camera_text"
android:text="@string/move_the_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>

View File

@ -63,6 +63,7 @@
<activity android:name=".StreetViewPanoramaEventsDemoActivity" />
<activity android:name=".StreetViewPanoramaViewDemoActivity" />
<activity android:name=".LiteDemoActivity" />
<activity android:name=".EventsDemoActivity" />
</application>
</manifest>

View File

@ -49,6 +49,12 @@
<string name="close_info_window_demo_label">Close Info Window Demo</string>
<string name="close_info_window_demo_details">Demonstrates how to close the info window when the currently selected marker is retapped.</string>
<!-- Events Demo -->
<string name="events_demo_label">Events</string>
<string name="events_demo_details">Demonstrates event handling.</string>
<string name="move_the_camera">Move the camera</string>
<string name="tap_instructions">Tap or long press on the map</string>
<!-- Layers Demo -->
<string name="buildings">Buildings</string>
<string name="hybrid">Hybrid</string>