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