Skip to content

Commit a6d24c6

Browse files
authored
chore: Add MyLocationDemoActivity in Kotlin (googlemaps-samples#239)
Includes region tags for: * maps_check_location_permission_result * maps_check_location_permission Also adds PermissionUtils in Kotlin Also apply following changes to Java version: * Refactor variables to remove Hungarian notation Also remove redundant schema from layout
1 parent b27512a commit a6d24c6

File tree

9 files changed

+372
-20
lines changed

9 files changed

+372
-20
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public class MyLocationDemoActivity extends AppCompatActivity
5656
* Flag indicating whether a requested permission has been denied after returning in
5757
* {@link #onRequestPermissionsResult(int, String[], int[])}.
5858
*/
59-
private boolean mPermissionDenied = false;
59+
private boolean permissionDenied = false;
6060

61-
private GoogleMap mMap;
61+
private GoogleMap map;
6262

6363
@Override
6464
protected void onCreate(Bundle savedInstanceState) {
@@ -71,11 +71,10 @@ protected void onCreate(Bundle savedInstanceState) {
7171
}
7272

7373
@Override
74-
public void onMapReady(GoogleMap map) {
75-
mMap = map;
76-
77-
mMap.setOnMyLocationButtonClickListener(this);
78-
mMap.setOnMyLocationClickListener(this);
74+
public void onMapReady(GoogleMap googleMap) {
75+
map = googleMap;
76+
map.setOnMyLocationButtonClickListener(this);
77+
map.setOnMyLocationClickListener(this);
7978
enableMyLocation();
8079
}
8180

@@ -86,8 +85,8 @@ private void enableMyLocation() {
8685
// [START maps_check_location_permission]
8786
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
8887
== PackageManager.PERMISSION_GRANTED) {
89-
if (mMap != null) {
90-
mMap.setMyLocationEnabled(true);
88+
if (map != null) {
89+
map.setMyLocationEnabled(true);
9190
}
9291
} else {
9392
// Permission to access the location is missing. Show rationale and request permission
@@ -124,7 +123,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
124123
// Permission was denied. Display an error message
125124
// [START_EXCLUDE]
126125
// Display the missing permission error dialog when the fragments resume.
127-
mPermissionDenied = true;
126+
permissionDenied = true;
128127
// [END_EXCLUDE]
129128
}
130129
}
@@ -133,10 +132,10 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
133132
@Override
134133
protected void onResumeFragments() {
135134
super.onResumeFragments();
136-
if (mPermissionDenied) {
135+
if (permissionDenied) {
137136
// Permission was not granted, display error dialog.
138137
showMissingPermissionError();
139-
mPermissionDenied = false;
138+
permissionDenied = false;
140139
}
141140
}
142141

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static class PermissionDeniedDialog extends DialogFragment {
7272

7373
private static final String ARGUMENT_FINISH_ACTIVITY = "finish";
7474

75-
private boolean mFinishActivity = false;
75+
private boolean finishActivity = false;
7676

7777
/**
7878
* Creates a new instance of this dialog and optionally finishes the calling Activity
@@ -89,7 +89,7 @@ public static PermissionDeniedDialog newInstance(boolean finishActivity) {
8989

9090
@Override
9191
public Dialog onCreateDialog(Bundle savedInstanceState) {
92-
mFinishActivity = getArguments().getBoolean(ARGUMENT_FINISH_ACTIVITY);
92+
finishActivity = getArguments().getBoolean(ARGUMENT_FINISH_ACTIVITY);
9393

9494
return new AlertDialog.Builder(getActivity())
9595
.setMessage(R.string.location_permission_denied)
@@ -100,7 +100,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
100100
@Override
101101
public void onDismiss(DialogInterface dialog) {
102102
super.onDismiss(dialog);
103-
if (mFinishActivity) {
103+
if (finishActivity) {
104104
Toast.makeText(getActivity(), R.string.permission_required_toast,
105105
Toast.LENGTH_SHORT).show();
106106
getActivity().finish();
@@ -122,7 +122,7 @@ public static class RationaleDialog extends DialogFragment {
122122

123123
private static final String ARGUMENT_FINISH_ACTIVITY = "finish";
124124

125-
private boolean mFinishActivity = false;
125+
private boolean finishActivity = false;
126126

127127
/**
128128
* Creates a new instance of a dialog displaying the rationale for the use of the location
@@ -149,7 +149,7 @@ public static RationaleDialog newInstance(int requestCode, boolean finishActivit
149149
public Dialog onCreateDialog(Bundle savedInstanceState) {
150150
Bundle arguments = getArguments();
151151
final int requestCode = arguments.getInt(ARGUMENT_PERMISSION_REQUEST_CODE);
152-
mFinishActivity = arguments.getBoolean(ARGUMENT_FINISH_ACTIVITY);
152+
finishActivity = arguments.getBoolean(ARGUMENT_FINISH_ACTIVITY);
153153

154154
return new AlertDialog.Builder(getActivity())
155155
.setMessage(R.string.permission_rationale_location)
@@ -161,7 +161,7 @@ public void onClick(DialogInterface dialog, int which) {
161161
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
162162
requestCode);
163163
// Do not finish the Activity while requesting permission.
164-
mFinishActivity = false;
164+
finishActivity = false;
165165
}
166166
})
167167
.setNegativeButton(android.R.string.cancel, null)
@@ -171,7 +171,7 @@ public void onClick(DialogInterface dialog, int which) {
171171
@Override
172172
public void onDismiss(DialogInterface dialog) {
173173
super.onDismiss(dialog);
174-
if (mFinishActivity) {
174+
if (finishActivity) {
175175
Toast.makeText(getActivity(),
176176
R.string.permission_required_toast,
177177
Toast.LENGTH_SHORT)

ApiDemos/java/app/src/gms/res/layout/my_location_demo.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
<fragment
2323
android:id="@+id/map"
2424
class="com.google.android.gms.maps.SupportMapFragment"
25-
xmlns:android="http://schemas.android.com/apk/res/android"
2625
android:layout_width="match_parent"
2726
android:layout_height="match_parent" />
2827
</FrameLayout>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class DemoDetailsList {
4949
LiteListDemoActivity::class.java),
5050
DemoDetails(R.string.markers_demo_label, R.string.markers_demo_description,
5151
MarkerDemoActivity::class.java),
52+
DemoDetails(R.string.my_location_demo_label, R.string.my_location_demo_details,
53+
MyLocationDemoActivity::class.java),
5254
DemoDetails(R.string.polygon_demo_label, R.string.polygon_demo_details,
5355
PolygonDemoActivity::class.java),
5456
DemoDetails(R.string.polyline_demo_label, R.string.polyline_demo_description,
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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.Manifest
17+
import android.content.pm.PackageManager
18+
import android.location.Location
19+
import android.os.Bundle
20+
import android.widget.Toast
21+
import androidx.appcompat.app.AppCompatActivity
22+
import androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback
23+
import androidx.core.content.ContextCompat
24+
import com.example.kotlindemos.PermissionUtils.PermissionDeniedDialog.Companion.newInstance
25+
import com.example.kotlindemos.PermissionUtils.isPermissionGranted
26+
import com.example.kotlindemos.PermissionUtils.requestPermission
27+
import com.google.android.gms.maps.GoogleMap
28+
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener
29+
import com.google.android.gms.maps.GoogleMap.OnMyLocationClickListener
30+
import com.google.android.gms.maps.OnMapReadyCallback
31+
import com.google.android.gms.maps.SupportMapFragment
32+
33+
/**
34+
* This demo shows how GMS Location can be used to check for changes to the users location. The
35+
* "My Location" button uses GMS Location to set the blue dot representing the users location.
36+
* Permission for [Manifest.permission.ACCESS_FINE_LOCATION] is requested at run
37+
* time. If the permission has not been granted, the Activity is finished with an error message.
38+
*/
39+
class MyLocationDemoActivity : AppCompatActivity(), OnMyLocationButtonClickListener,
40+
OnMyLocationClickListener, OnMapReadyCallback, OnRequestPermissionsResultCallback {
41+
/**
42+
* Flag indicating whether a requested permission has been denied after returning in
43+
* [.onRequestPermissionsResult].
44+
*/
45+
private var permissionDenied = false
46+
private lateinit var map: GoogleMap
47+
override fun onCreate(savedInstanceState: Bundle?) {
48+
super.onCreate(savedInstanceState)
49+
setContentView(R.layout.my_location_demo)
50+
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
51+
mapFragment?.getMapAsync(this)
52+
}
53+
54+
override fun onMapReady(googleMap: GoogleMap?) {
55+
map = googleMap ?: return
56+
googleMap.setOnMyLocationButtonClickListener(this)
57+
googleMap.setOnMyLocationClickListener(this)
58+
enableMyLocation()
59+
}
60+
61+
/**
62+
* Enables the My Location layer if the fine location permission has been granted.
63+
*/
64+
private fun enableMyLocation() {
65+
if (!::map.isInitialized) return
66+
// [START maps_check_location_permission]
67+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
68+
== PackageManager.PERMISSION_GRANTED) {
69+
map.isMyLocationEnabled = true
70+
} else {
71+
// Permission to access the location is missing. Show rationale and request permission
72+
requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
73+
Manifest.permission.ACCESS_FINE_LOCATION, true
74+
)
75+
}
76+
// [END maps_check_location_permission]
77+
}
78+
79+
override fun onMyLocationButtonClick(): Boolean {
80+
Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show()
81+
// Return false so that we don't consume the event and the default behavior still occurs
82+
// (the camera animates to the user's current position).
83+
return false
84+
}
85+
86+
override fun onMyLocationClick(location: Location) {
87+
Toast.makeText(this, "Current location:\n$location", Toast.LENGTH_LONG).show()
88+
}
89+
90+
// [START maps_check_location_permission_result]
91+
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
92+
if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
93+
return
94+
}
95+
if (isPermissionGranted(permissions, grantResults, Manifest.permission.ACCESS_FINE_LOCATION)) {
96+
// Enable the my location layer if the permission has been granted.
97+
enableMyLocation()
98+
} else {
99+
// Permission was denied. Display an error message
100+
// [START_EXCLUDE]
101+
// Display the missing permission error dialog when the fragments resume.
102+
permissionDenied = true
103+
// [END_EXCLUDE]
104+
}
105+
}
106+
107+
// [END maps_check_location_permission_result]
108+
override fun onResumeFragments() {
109+
super.onResumeFragments()
110+
if (permissionDenied) {
111+
// Permission was not granted, display error dialog.
112+
showMissingPermissionError()
113+
permissionDenied = false
114+
}
115+
}
116+
117+
/**
118+
* Displays a dialog with error message explaining that the location permission is missing.
119+
*/
120+
private fun showMissingPermissionError() {
121+
newInstance(true).show(supportFragmentManager, "dialog")
122+
}
123+
124+
companion object {
125+
/**
126+
* Request code for location permission request.
127+
*
128+
* @see .onRequestPermissionsResult
129+
*/
130+
private const val LOCATION_PERMISSION_REQUEST_CODE = 1
131+
}
132+
}

0 commit comments

Comments
 (0)