質問編集履歴

2

XMLの追加

2021/12/11 17:36

投稿

MM_LL
MM_LL

スコア18

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,161 @@
1
1
  ```ここに言語を入力
2
2
 
3
+ <LinearLayout
4
+
5
+ android:id="@+id/linearLayout"
6
+
7
+ android:layout_width="match_parent"
8
+
9
+ android:layout_height="wrap_content"
10
+
11
+ android:orientation="horizontal"
12
+
13
+ app:layout_constraintEnd_toEndOf="parent"
14
+
15
+ app:layout_constraintStart_toStartOf="parent"
16
+
17
+ app:layout_constraintTop_toTopOf="parent">
18
+
19
+
20
+
21
+ <EditText
22
+
23
+ android:id="@+id/et"
24
+
25
+ android:layout_width="wrap_content"
26
+
27
+ android:layout_height="wrap_content"
28
+
29
+ android:layout_weight="1"
30
+
31
+ android:ems="10"
32
+
33
+ android:hint="入力"
34
+
35
+ android:inputType="textPersonName" />
36
+
37
+
38
+
39
+ <Button
40
+
41
+ android:id="@+id/btMap1"
42
+
43
+ android:layout_width="wrap_content"
44
+
45
+ android:layout_height="wrap_content"
46
+
47
+ android:layout_weight="1"
48
+
49
+ android:onClick="btMap1"
50
+
51
+ android:text="検索" />
52
+
53
+ </LinearLayout>
54
+
55
+
56
+
57
+ <LinearLayout
58
+
59
+ android:layout_width="match_parent"
60
+
61
+ android:layout_height="681dp"
62
+
63
+ android:orientation="horizontal"
64
+
65
+ app:layout_constraintEnd_toEndOf="parent"
66
+
67
+ app:layout_constraintStart_toStartOf="parent"
68
+
69
+ app:layout_constraintTop_toBottomOf="@+id/linearLayout">
70
+
71
+
72
+
73
+ <TextView
74
+
75
+ android:layout_width="wrap_content"
76
+
77
+ android:layout_height="wrap_content"
78
+
79
+ android:layout_marginRight="5dp"
80
+
81
+ android:text="現在地" />
82
+
83
+
84
+
85
+ <TextView
86
+
87
+ android:layout_width="wrap_content"
88
+
89
+ android:layout_height="wrap_content"
90
+
91
+ android:layout_marginRight="5dp"
92
+
93
+ android:text="緯度:" />
94
+
95
+
96
+
97
+ <TextView
98
+
99
+ android:id="@+id/tvMap1"
100
+
101
+ android:layout_width="wrap_content"
102
+
103
+ android:layout_height="wrap_content"
104
+
105
+ android:layout_marginRight="5dp"
106
+
107
+ android:layout_weight="0.5"
108
+
109
+ android:maxLines="1"/>
110
+
111
+
112
+
113
+ <TextView
114
+
115
+ android:layout_width="wrap_content"
116
+
117
+ android:layout_height="wrap_content"
118
+
119
+ android:layout_weight="1"
120
+
121
+ android:text="経度:" />
122
+
123
+
124
+
125
+ <TextView
126
+
127
+ android:id="@+id/tvMap2"
128
+
129
+ android:layout_width="wrap_content"
130
+
131
+ android:layout_height="wrap_content"
132
+
133
+ android:layout_weight="0.5"
134
+
135
+ android:maxLines="1"/>
136
+
137
+
138
+
139
+ <Button
140
+
141
+ android:id="@+id/btMap2"
142
+
143
+ android:layout_width="72dp"
144
+
145
+ android:layout_height="wrap_content"
146
+
147
+ android:layout_weight="1"
148
+
149
+ android:onClick="btMap2"
150
+
151
+ android:text="地図表示" />
152
+
153
+ </LinearLayout>
154
+
155
+
156
+
157
+ ``````ここに言語を入力
158
+
3
159
  package com.test.mapapp
4
160
 
5
161
 

1

MainActivityの追加

2021/12/11 17:36

投稿

MM_LL
MM_LL

スコア18

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,214 @@
1
+ ```ここに言語を入力
2
+
3
+ package com.test.mapapp
4
+
5
+
6
+
7
+ import android.Manifest
8
+
9
+ import android.content.Context
10
+
11
+ import android.content.Intent
12
+
13
+ import android.content.pm.PackageManager
14
+
15
+ import android.location.LocationManager
16
+
17
+ import android.net.Uri
18
+
19
+ import android.os.Bundle
20
+
21
+ import android.view.View
22
+
23
+ import android.widget.EditText
24
+
25
+ import android.widget.TextView
26
+
27
+ import androidx.appcompat.app.AppCompatActivity
28
+
29
+ import androidx.core.app.ActivityCompat
30
+
31
+ import com.google.android.gms.location.*
32
+
33
+ import java.net.URLEncoder
34
+
35
+
36
+
37
+
38
+
39
+ class MainActivity : AppCompatActivity() {
40
+
41
+ private var _latitude = 0.0
42
+
43
+
44
+
45
+ private var _longitude = 0.0
46
+
47
+
48
+
49
+ private lateinit var _fusedLocationClient: FusedLocationProviderClient
50
+
51
+
52
+
53
+ private lateinit var _locationRequest: LocationRequest
54
+
55
+
56
+
57
+ private lateinit var _onUpdateLocation: OnUpdateLocation
58
+
59
+
60
+
61
+
62
+
63
+ override fun onCreate(savedInstanceState: Bundle?) {
64
+
65
+ super.onCreate(savedInstanceState)
66
+
67
+ setContentView(R.layout.activity_main)
68
+
69
+
70
+
71
+ _fusedLocationClient = LocationServices.getFusedLocationProviderClient(this@MainActivity)
72
+
73
+
74
+
75
+ _locationRequest = LocationRequest.create()
76
+
77
+
78
+
79
+ _locationRequest?.let {
80
+
81
+
82
+
83
+ it.interval = 5000
84
+
85
+
86
+
87
+ it.fastestInterval = 1000
88
+
89
+
90
+
91
+ it.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
92
+
93
+ }
94
+
95
+
96
+
97
+ _onUpdateLocation = OnUpdateLocation()
98
+
99
+ }
100
+
101
+
102
+
103
+ override fun onResume() {
104
+
105
+ super.onResume()
106
+
107
+ if(ActivityCompat.checkSelfPermission(this@MainActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
108
+
109
+
110
+
111
+ val permissions = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)
112
+
113
+ ActivityCompat.requestPermissions(this@MainActivity, permissions, 1000)
114
+
115
+
116
+
117
+ return
118
+
119
+ }
120
+
121
+ _fusedLocationClient.requestLocationUpdates(_locationRequest, _onUpdateLocation, mainLooper)
122
+
123
+ }
124
+
125
+
126
+
127
+ override fun onPause() {
128
+
129
+ super.onPause()
130
+
131
+
132
+
133
+ _fusedLocationClient.removeLocationUpdates(_onUpdateLocation)
134
+
135
+ }
136
+
137
+
138
+
139
+ fun btMap1 (view: View) {
140
+
141
+ val et = findViewById<EditText>(R.id.et)
142
+
143
+ var searchWord = et.text.toString()
144
+
145
+ searchWord = URLEncoder.encode(searchWord, "UTF-8")
146
+
147
+ val uriStr = "geo:0,0?q=${searchWord}"
148
+
149
+ val uri = Uri.parse(uriStr)
150
+
151
+ val intent = Intent(Intent.ACTION_VIEW, uri)
152
+
153
+ startActivity(intent)
154
+
155
+ }
156
+
157
+ fun btMap2(view: View) {
158
+
159
+ val uriStr ="geo:${_latitude},${_longitude}"
160
+
161
+ val uri = Uri.parse(uriStr)
162
+
163
+ val intent = Intent(Intent.ACTION_VIEW,uri)
164
+
165
+ startActivity(intent)
166
+
167
+ }
168
+
169
+ private inner class OnUpdateLocation : LocationCallback() {
170
+
171
+ override fun onLocationResult(locationResult: LocationResult?) {
172
+
173
+ locationResult?.let {
174
+
175
+
176
+
177
+ val location = it.lastLocation
178
+
179
+ location?.let {
180
+
181
+
182
+
183
+ _latitude = it.latitude
184
+
185
+
186
+
187
+ _longitude = it.longitude
188
+
189
+ val tvMap1:TextView =findViewById(R.id.tvMap1)
190
+
191
+ val tvMap2:TextView =findViewById(R.id.tvMap2)
192
+
193
+ tvMap1.text = _latitude.toString()
194
+
195
+ tvMap2.text = _longitude.toString()
196
+
197
+ }
198
+
199
+ }
200
+
201
+ }
202
+
203
+ }
204
+
205
+
206
+
207
+ }
208
+
209
+
210
+
1
- ![イメージ説明](072006b6c7c6de4ba6276ebc6f04991e.png)
211
+ ```![イメージ説明](072006b6c7c6de4ba6276ebc6f04991e.png)
2
212
 
3
213
 
4
214