GPS を利用した現在位置検出

package jp.hews.hellogps;

/*
 * GPS を利用して現在位置を取得する
 * 2011.03.28
 */
import java.io.IOException;
import java.util.Locale;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.Geocoder;
import android.location.Address;

import android.widget.TextView;

import android.util.Log;

public class HelloGPSActivity extends Activity implements LocationListener {
	//位置情報の通知範囲
	static final int MIN_TIME = 2000;	//通知の最小時間 [ミリ秒]
	static final float MIN_DISTANCE = 1;	//通知の最短距離[メートル]
	
	//位置情報を取得するためのマネージャ
	LocationManager mLocationManager;
	
	//地名の検索用クラス
	Geocoder mGeocoder;	//緯度・経度から地名への変換

	//地名の表示用テキストビュー
	TextView mStatusText;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView tv = (TextView)findViewById(R.id.status_text);
        tv.setBackgroundColor(Color.GRAY);
        
        //地名の検索用クラス
        mGeocoder = new Geocoder(getApplicationContext(), Locale.JAPAN);
        mStatusText = (TextView)findViewById(R.id.status_text);
        
        //位置情報取得用のクラス(GPS との通信用)
        mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    }

    //onResume メソッド:アクティビティがフォアグラウンドになる時に呼び出される.
    @Override
    protected void onResume(){
    	super.onResume();
    	//最後に取得した位置があれば表示する
    	Location loc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    	if(loc != null){
    		onLocationChanged(loc);	//地図の表示
    	}
    	//現在位置が変化したらメソッドが呼び出されるように登録する
    	mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
    			MIN_TIME, MIN_DISTANCE, this);
    }

    //onPause メソッド:アクティビティがフォアグラウンドでなくなる時に呼び出される.
    //LocationManager.removeUpdates() は requestLocationUpdates() と対である.
    @Override
    protected void onPause(){
    	super.onPause();
    	//位置情報の登録を解除する
    	mLocationManager.removeUpdates(this);
    }
    
	@Override
	public void onLocationChanged(Location location) {
		//現在の地名を表示
		showLocationName(location);
	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		//プロバイダが使用不可になった
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		//プロバイダが使用可能になった
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		//プロバイダの状態が変化した
	}
	
	//GeoPoint の位置の地名を表示する
	private void showLocationName(Location loc){
		//座標の変換
		double latitude = loc.getLatitude();
		double longtitude = loc.getLongitude();
		
		//GeoCoder を用いて地名を検索
		StringBuffer buff = new StringBuffer();
		try {
			List<Address> addrs = mGeocoder.getFromLocation(latitude, longtitude, 1);
			for(Address addr : addrs){
				//地名を取得して,文字列に連結する
				int index = addr.getMaxAddressLineIndex();
				for(int i = 0; i <= index; i++){
					buff.append(addr.getAddressLine(i));
					buff.append("\n");
				}
				buff.append("\n");
			}
		} catch(IOException e){
			Log.e("HelloLocationActivity", e.toString());
		}
		
		//テキストの表示
		mStatusText.setText(buff.toString());
	}
}