Last active
September 8, 2017 10:20
-
-
Save amay077/5635546 to your computer and use it in GitHub Desktop.
Google I/O 2013 で発表された Fused Location Provider を使ってみる ref: http://qiita.com/amay077/items/b1849c6377f70d2060e0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
| package="com.example.fusedlocationprovidersample" | |
| android:versionCode="1" | |
| android:versionName="1.0" > | |
| <uses-sdk | |
| android:minSdkVersion="8" | |
| android:targetSdkVersion="17" /> | |
| <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> | |
| <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> | |
| <application | |
| android:allowBackup="true" | |
| android:icon="@drawable/ic_launcher" | |
| android:label="@string/app_name" | |
| android:theme="@style/AppTheme" > | |
| <activity | |
| android:name="com.example.fusedlocationprovidersample.MainActivity" | |
| android:screenOrientation="portrait" | |
| android:label="@string/app_name" > | |
| <intent-filter> | |
| <action android:name="android.intent.action.MAIN" /> | |
| <category android:name="android.intent.category.LAUNCHER" /> | |
| </intent-filter> | |
| </activity> | |
| </application> | |
| </manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.fusedlocationprovidersample; | |
| import com.google.android.gms.common.ConnectionResult; | |
| import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks; | |
| import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener; | |
| import com.google.android.gms.location.LocationClient; | |
| import com.google.android.gms.location.LocationListener; | |
| import com.google.android.gms.location.LocationRequest; | |
| import android.location.Location; | |
| import android.os.Bundle; | |
| import android.text.format.DateFormat; | |
| import android.view.View; | |
| import android.view.View.OnClickListener; | |
| import android.widget.Button; | |
| import android.widget.TextView; | |
| import android.app.Activity; | |
| public class MainActivity extends Activity { | |
| // FusedLocationProvider 用の Client | |
| private LocationClient _locationClient; | |
| private TextView _textResult; | |
| // 以前とは package が違う LocationListener | |
| private final LocationListener _locationListener = new LocationListener() { | |
| @Override | |
| public void onLocationChanged(final Location location) { | |
| MainActivity.this.runOnUiThread(new Runnable() { | |
| @Override | |
| public void run() { | |
| String text = _textResult.getText().toString(); | |
| text = DateFormat.format("hh:mm:ss.sss", location.getTime()) + " - " | |
| + location.getLatitude() + "/" + | |
| + location.getLongitude() + "/" + | |
| + location.getAccuracy() + | |
| "\n" + text; | |
| _textResult.setText(text); | |
| } | |
| }); | |
| } | |
| }; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| _textResult = (TextView)findViewById(R.id.text_result); | |
| final Button buttonLocate = (Button)findViewById(R.id.button_locate); | |
| buttonLocate.setOnClickListener(new OnClickListener() { | |
| private boolean _isStarted = false; | |
| @Override | |
| public void onClick(View v) { | |
| if (!_isStarted) { | |
| startLocate(); | |
| buttonLocate.setText("Stop"); | |
| } else { | |
| stopLocate(); | |
| buttonLocate.setText("Start"); | |
| } | |
| _isStarted = !_isStarted; | |
| } | |
| }); | |
| } | |
| @Override | |
| protected void onDestroy() { | |
| stopLocate(); | |
| super.onDestroy(); | |
| } | |
| private void startLocate() { | |
| _locationClient = new LocationClient(this, new ConnectionCallbacks() { | |
| @Override | |
| public void onConnected(Bundle bundle) { | |
| // 2. 位置の取得開始! | |
| LocationRequest request = LocationRequest.create() | |
| .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) | |
| .setInterval(5000); // 5秒おき | |
| _locationClient.requestLocationUpdates(request, _locationListener); | |
| } | |
| @Override | |
| public void onDisconnected() { | |
| _locationClient = null; | |
| } | |
| }, new OnConnectionFailedListener() { | |
| @Override | |
| public void onConnectionFailed(ConnectionResult result) { | |
| } | |
| }); | |
| // 1. 位置取得サービスに接続! | |
| _locationClient.connect(); | |
| } | |
| private void stopLocate() { | |
| if (_locationClient == null || !_locationClient.isConnected()) { | |
| return; | |
| } | |
| _locationClient.removeLocationUpdates(_locationListener); | |
| _locationClient.disconnect(); | |
| // ConnectionCallbacks.onDisconnected が呼ばれるまで待った方がいい気がする | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment