質問編集履歴

1

コードのついか

2017/07/28 04:38

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -9,3 +9,341 @@
9
9
 
10
10
 
11
11
  どうすればいいでしょうか?
12
+
13
+
14
+
15
+ ```java
16
+
17
+
18
+
19
+
20
+
21
+ public class LocationActivity extends FragmentActivity implements
22
+
23
+ GoogleApiClient.ConnectionCallbacks,
24
+
25
+ GoogleApiClient.OnConnectionFailedListener,
26
+
27
+ LocationListener {
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+ @Override
38
+
39
+ protected void onCreate(Bundle savedInstanceState) {
40
+
41
+ super.onCreate(savedInstanceState);
42
+
43
+ setContentView(R.layout.activity_main);
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+ // LocationRequest を生成して精度、インターバルを設定
52
+
53
+ locationRequest = LocationRequest.create();
54
+
55
+
56
+
57
+ locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
58
+
59
+ locationRequest.setInterval(50000);
60
+
61
+ locationRequest.setFastestInterval(1000);
62
+
63
+ locationRequest.setSmallestDisplacement(1);
64
+
65
+
66
+
67
+ // locationRequest.setInterval(5000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
68
+
69
+ //locationRequest.setExpirationDuration();このリクエストの期間をミリ秒単位
70
+
71
+ //locationRequest.setExpirationTime();起動後の要求の有効期限
72
+
73
+ //locationRequest.setMaxWaitTime();複数の場所を一度に配信できます。
74
+
75
+ //locationRequest.setNumUpdates();新しい場所が1つだけ必要な場合
76
+
77
+ //locationRequest.setPriority(1);//要求の優先順位を設定
78
+
79
+
80
+
81
+
82
+
83
+ fusedLocationProviderApi = LocationServices.FusedLocationApi;
84
+
85
+
86
+
87
+ mGoogleApiClient = new GoogleApiClient.Builder(this)
88
+
89
+ .addApi(LocationServices.API)
90
+
91
+ .addConnectionCallbacks(this)
92
+
93
+ .addOnConnectionFailedListener(this)
94
+
95
+ .build();
96
+
97
+
98
+
99
+ // 測位開始
100
+
101
+ Button buttonStart = (Button)findViewById(R.id.button_start);
102
+
103
+ buttonStart.setOnClickListener(new View.OnClickListener() {
104
+
105
+ @Override
106
+
107
+ public void onClick(View v) {
108
+
109
+ startFusedLocation();
110
+
111
+ }
112
+
113
+ });
114
+
115
+
116
+
117
+ // 測位終了
118
+
119
+ Button buttonStop = (Button)findViewById(R.id.button_stop);
120
+
121
+ buttonStop.setOnClickListener(new View.OnClickListener() {
122
+
123
+ @Override
124
+
125
+ public void onClick(View v) {
126
+
127
+ stopFusedLocation();
128
+
129
+ }
130
+
131
+ });
132
+
133
+
134
+
135
+ }
136
+
137
+
138
+
139
+
140
+
141
+ private void startFusedLocation(){
142
+
143
+ if (!mResolvingError) {
144
+
145
+ mGoogleApiClient.connect();
146
+
147
+
148
+
149
+ } else {
150
+
151
+ }
152
+
153
+
154
+
155
+ }
156
+
157
+
158
+
159
+ private void stopFusedLocation(){
160
+
161
+ // Disconnecting the client invalidates it.
162
+
163
+ mGoogleApiClient.disconnect();
164
+
165
+ }
166
+
167
+
168
+
169
+ @Override
170
+
171
+ protected void onStart() {
172
+
173
+ super.onStart();
174
+
175
+
176
+
177
+ }
178
+
179
+
180
+
181
+ @Override
182
+
183
+ protected void onStop() {
184
+
185
+ super.onStop();
186
+
187
+ stopFusedLocation();
188
+
189
+ }
190
+
191
+
192
+
193
+
194
+
195
+ @Override
196
+
197
+ public void onConnected(Bundle bundle) {
198
+
199
+
200
+
201
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
202
+
203
+ return;
204
+
205
+ }
206
+
207
+ Location currentLocation = fusedLocationProviderApi.getLastLocation(mGoogleApiClient);
208
+
209
+
210
+
211
+ if (currentLocation != null && currentLocation.getTime() > 20000) {
212
+
213
+ location = currentLocation;
214
+
215
+
216
+
217
+ textLog += "---------- onConnected \n";
218
+
219
+ textLog += "Latitude=" + String.valueOf(location.getLatitude()) + "\n";
220
+
221
+ textLog += "Longitude=" + String.valueOf(location.getLongitude()) + "\n";
222
+
223
+ textLog += "Accuracy=" + String.valueOf(location.getAccuracy()) + "\n";
224
+
225
+ textLog += "Altitude=" + String.valueOf(location.getAltitude()) + "\n";
226
+
227
+ textLog += "Time=" + String.valueOf(location.getTime()) + "\n";
228
+
229
+ textLog += "Speed=" + String.valueOf(location.getSpeed()) + "\n";
230
+
231
+ textLog += "Bearing=" + String.valueOf(location.getBearing()) + "\n";
232
+
233
+ textView.setText(textLog);
234
+
235
+
236
+
237
+
238
+
239
+ } else {
240
+
241
+ // バックグラウンドから戻ってしまうと例外が発生する場合がある
242
+
243
+ try {
244
+
245
+
246
+
247
+ fusedLocationProviderApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
248
+
249
+ Executors.newScheduledThreadPool(1).schedule(new Runnable() {
250
+
251
+ @Override
252
+
253
+ public void run() {
254
+
255
+ fusedLocationProviderApi.removeLocationUpdates(mGoogleApiClient, LocationActivity.this);
256
+
257
+ }
258
+
259
+ }, 60000, TimeUnit.MILLISECONDS);
260
+
261
+
262
+
263
+ textLog += "onConnected(), requestLocationUpdates \n";
264
+
265
+ textView.setText(textLog);
266
+
267
+
268
+
269
+ } catch (Exception e) {
270
+
271
+ e.printStackTrace();
272
+
273
+ finish();
274
+
275
+ }
276
+
277
+ }
278
+
279
+ }
280
+
281
+
282
+
283
+
284
+
285
+ @Override
286
+
287
+ public void onLocationChanged(Location location) {
288
+
289
+ lastLocationTime = location.getTime() - lastLocationTime;
290
+
291
+
292
+
293
+ textLog += "2---------- onLocationChanged \n";
294
+
295
+ textLog += "2Latitude=" + String.valueOf(location.getLatitude()) + "\n";
296
+
297
+ textLog += "2Longitude=" + String.valueOf(location.getLongitude()) + "\n";
298
+
299
+ textLog += "2Accuracy=" + String.valueOf(location.getAccuracy()) + "\n";
300
+
301
+ textLog += "2Altitude=" + String.valueOf(location.getAltitude()) + "\n";
302
+
303
+ textLog += "2Time=" + String.valueOf(location.getTime()) + "\n";
304
+
305
+ textLog += "2Speed=" + String.valueOf(location.getSpeed()) + "\n";
306
+
307
+ textLog += "2Bearing=" + String.valueOf(location.getBearing()) + "\n";
308
+
309
+ textLog += "2time= " + String.valueOf(lastLocationTime) + " msec \n";
310
+
311
+ textView.setText(textLog);
312
+
313
+ }
314
+
315
+
316
+
317
+ @Override
318
+
319
+ public void onConnectionSuspended(int i) {
320
+
321
+ }
322
+
323
+
324
+
325
+ @Override
326
+
327
+ public void onConnectionFailed(ConnectionResult connectionResult) {
328
+
329
+
330
+
331
+ if (mResolvingError) {
332
+
333
+
334
+
335
+ return;
336
+
337
+ } else if (connectionResult.hasResolution()) {
338
+
339
+
340
+
341
+ } else {
342
+
343
+ mResolvingError = true;
344
+
345
+ }
346
+
347
+ }
348
+
349
+ ```