質問編集履歴

2

a

2021/07/16 08:42

投稿

universegift77
universegift77

スコア12

test CHANGED
File without changes
test CHANGED
@@ -71,85 +71,3 @@
71
71
  </manifest>
72
72
 
73
73
  ```
74
-
75
-
76
-
77
- ```kotlin
78
-
79
- //gps data
80
-
81
- var previousLocation =""
82
-
83
- var permissionFlag =true
84
-
85
- while (permissionFlag){
86
-
87
- if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED){
88
-
89
- permissionFlag=false
90
-
91
- Toast.makeText(this,"permission accepted",Toast.LENGTH_SHORT).show()
92
-
93
- val locationCallback=object : LocationCallback(){
94
-
95
- override fun onLocationResult(locationResult: LocationResult?){
96
-
97
- val location=locationResult?.lastLocation?:return
98
-
99
- Toast.makeText(this@MainActivity,"緯度:${location.latitude}, 経度:${location.longitude}",Toast.LENGTH_SHORT).show()
100
-
101
- }
102
-
103
- }
104
-
105
- val locationRequest=LocationRequest.create().apply{
106
-
107
- interval=10000
108
-
109
- fastestInterval=5000
110
-
111
- priority=LocationRequest.PRIORITY_HIGH_ACCURACY
112
-
113
- }
114
-
115
-
116
-
117
- fusedLocationClient= FusedLocationProviderClient(this)
118
-
119
- fusedLocationClient.lastLocation
120
-
121
- .addOnSuccessListener{location: Location?->
122
-
123
- if(previousLocation!=location.toString() && location!=null){
124
-
125
- binding.geoTextview.text=location.toString()+"\n\n"+location.toString()
126
-
127
- previousLocation=location.toString()
128
-
129
- }
130
-
131
- }
132
-
133
- fusedLocationClient.requestLocationUpdates(locationRequest,locationCallback,Looper.myLooper())
134
-
135
- }else{
136
-
137
- Toast.makeText(this,"permission denied",Toast.LENGTH_SHORT).show()
138
-
139
- //ActivityCompat.shouldShowRequestPermissionRationale()
140
-
141
- ActivityCompat.requestPermissions(
142
-
143
- this,
144
-
145
- arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION),
146
-
147
- gpsRequestCode
148
-
149
- )
150
-
151
- }
152
-
153
- }
154
-
155
- ```

1

a

2021/07/16 08:41

投稿

universegift77
universegift77

スコア12

test CHANGED
File without changes
test CHANGED
@@ -71,3 +71,85 @@
71
71
  </manifest>
72
72
 
73
73
  ```
74
+
75
+
76
+
77
+ ```kotlin
78
+
79
+ //gps data
80
+
81
+ var previousLocation =""
82
+
83
+ var permissionFlag =true
84
+
85
+ while (permissionFlag){
86
+
87
+ if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED){
88
+
89
+ permissionFlag=false
90
+
91
+ Toast.makeText(this,"permission accepted",Toast.LENGTH_SHORT).show()
92
+
93
+ val locationCallback=object : LocationCallback(){
94
+
95
+ override fun onLocationResult(locationResult: LocationResult?){
96
+
97
+ val location=locationResult?.lastLocation?:return
98
+
99
+ Toast.makeText(this@MainActivity,"緯度:${location.latitude}, 経度:${location.longitude}",Toast.LENGTH_SHORT).show()
100
+
101
+ }
102
+
103
+ }
104
+
105
+ val locationRequest=LocationRequest.create().apply{
106
+
107
+ interval=10000
108
+
109
+ fastestInterval=5000
110
+
111
+ priority=LocationRequest.PRIORITY_HIGH_ACCURACY
112
+
113
+ }
114
+
115
+
116
+
117
+ fusedLocationClient= FusedLocationProviderClient(this)
118
+
119
+ fusedLocationClient.lastLocation
120
+
121
+ .addOnSuccessListener{location: Location?->
122
+
123
+ if(previousLocation!=location.toString() && location!=null){
124
+
125
+ binding.geoTextview.text=location.toString()+"\n\n"+location.toString()
126
+
127
+ previousLocation=location.toString()
128
+
129
+ }
130
+
131
+ }
132
+
133
+ fusedLocationClient.requestLocationUpdates(locationRequest,locationCallback,Looper.myLooper())
134
+
135
+ }else{
136
+
137
+ Toast.makeText(this,"permission denied",Toast.LENGTH_SHORT).show()
138
+
139
+ //ActivityCompat.shouldShowRequestPermissionRationale()
140
+
141
+ ActivityCompat.requestPermissions(
142
+
143
+ this,
144
+
145
+ arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION),
146
+
147
+ gpsRequestCode
148
+
149
+ )
150
+
151
+ }
152
+
153
+ }
154
+
155
+ ```