イベントリスナーを解除するにはLocationManagerクラスに定義されているremoveUpdates()メソッドを使用します。
private LocationListener listener; // ...省略... locationManager.removeUpdates(listener);

画面上部に位置情報の取得時間、取得した緯度および経度を表示し、画面下部に取得した位置情報を中心としたMAPを表示するアプリケーションです。
このアプリケーションではGoogle Maps Android v2を利用します。前のCHAPTERを参照し、セットアップしておいてください。
まずは、MainActivityのレイアウトXMLファイルです。
● res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="時間:" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="緯度:" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="経度:" />
<fragment
android:id="@+id/mapFragment1"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ロケーションプロバイダから取得した「時間」「緯度」「経度」を表示するためのTextViewと、MAPを表示するためのfragmentが定義しています。
次に、MainActivityのソースプログラムです。
● src/com.example.locationsample/MainActivity.java
package com.example.locationsample;
// … 省略 …
public class MainActivity extends FragmentActivity {
private GoogleMap map;
private LocationManager manager;
private LocationListener listener;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
final TextView timeText = (TextView) findViewById(R.id.textView1);
final TextView latText = (TextView) findViewById(R.id.textView2);
final TextView lngText = (TextView) findViewById(R.id.textView3);
FragmentManager fm = getSupportFragmentManager();
SupportMapFragment mapFragment =
(SupportMapFragment) fm.findFragmentById(R.id.mapFragment1);
map = mapFragment.getMap();
manager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onLocationChanged(Location location) {
timeText.setText("時間:" + sdf.format(location.getTime()));
latText.setText("緯度:" + location.getLatitude());
lngText.setText("経度:" + location.getLongitude());
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
map.addMarker(new MarkerOptions().position(latLng));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18.0f));
}
};
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 15000, 5, listener);
}
@Override
protected void onPause() {
super.onPause();
map.clear();
manager.removeUpdates(listener);
}
}
全体として、16行目から始まるonResume()メソッド内でリスナーの設定処理を行い、55行目から始まるonPause()メソッド内でリスナーの解除をしています。
これで、Activityがフォアグラウンド中にだけ位置情報を取得することになります。
まずは16行目から始まるリスナーの設定処理です。
23行目から26行目でMainActivity上に設置されているMapFragmentをインスタンス化し、GoogleMapオブジェクトを取得しています。
このオブジェクトを操作することで、マーカーを設置したり、MAPの中心を移動したりすることができるようになります。
次に28行目でLocationManagerオブジェクトの取得、29行目から50行目で匿名クラスを使用しLocationListenerインタフェースをインスタンス化しています。
LocationListenerインタフェースに定義されている4つのメソッドのうち、3つは空(何も実装していない)ですが、onLocationChanged()メソッドには処理を記述しました。
このonLocationChanged()メソッドの42行目から44行目で引数のLocationオブジェクトから時間、経度、緯度を取り出し、それぞれTextViewに設定しています。
46行目ではLatLngクラス(緯度と経度をラッピングするためのクラス)をインスタンス化し、そのオブジェクトを使用して47行目でMAPにマーカーを追加しています。
MAP上に描くマーカーを表すクラスMarkerOptionsには、位置を表すLatLngオブジェクトの他にも、タイトル(文字列)やスニペット(文字列)、アイコンなどを指定することができます。
48行目では、MAPの中心となる位置を指定しています。
これをGoogle MapではCameraの位置を動かすという考え方で実装しているため、メソッド名にmoveCameraという名前がついています。
48行目では、このメソッドの引数にCameraUpdateFactory.newLatLngZoom()メソッドの実行結果を渡しています。このnewLatLngZoom()メソッドは引数を2つとり、第1引数は緯度経度をラッピングしたLatLngオブジェクトを、第2引数にはMAPの倍率を2.0から21.0までのfloat値で指定します。
これでLocationListenerオブジェクトができました。あとは、51行目でrequestLocationUpdates()メソッドを呼び出すだけです。
ここでは、ネットワークプロバイダを使用し、時間間隔を15,000msに、距離間隔を5mに設定しています。
次に55行目から始まるリスナー解除処理です。
57行目で設定したマーカーを全てクリアし、58行目でリスナーの解除をしています。
最後に、マニフェストファイルです。
● AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationsample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.example.locationsample.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.locationsample.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="ここにAPIキーを設定する" />
<activity
android:name="com.example.locationsample.MainActivity"
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>
基本的にはマップのマニフェストファイルと変わりありません。
位置情報を利用するため、17行目もしくは18行目のパーミッションを忘れずに設定してください。