|
|||||
GPS位置情報の利用GPSから位置情報(緯度、経度、及び高度)を取得する。まず、getSystemServiceでLOCATION_SERVICEを指定してLocationManagerを取得する。そして、それを利用してrequestLocationUpdatesメソッドでLocationManager.GPS_PROVIDERを指定してGPSからの情報を要求する。 GPSからの情報が受信できない場合を考慮して、基地局からの位置情報(NETWORK_PROVIDER)も要求しておき、それらを選択的に利用する。下記の例ではGPSの位置情報を優先し、10秒を超えてGPSからの位置情報が受信できない場合、基地局からの情報を利用する。通常は屋内でプログラムを書く人のほうが多いと思うので、屋内でも何らかの情報が得られるようにするためである。 これらの情報を取得するためにはメインActivityでLocationListenerをimplementsする。オーバライドしたonLocationChangedイベントで位置情報を表示する。この時、GPSからの情報の精度は基地局からの情報の精度よりも高いものとしてGPSからの情報を優先して表示する。尚、GPSを利用する場合、AndroidManifest.xmlでACCESS_FINE_LOCATIONパーミッションを予め付与しておく必要がある。
package biz.office_matsunaga.LocationTest;
import java.text.SimpleDateFormat;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class LocationTestActivity extends Activity implements LocationListener {
LocationManager locationManager;
long lastUpdate = 0;
String lastProvider = "";
TextView textView2;
TextView textView4;
TextView textView6;
TextView textView8;
TextView textView10;
TextView textView12;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView2 = (TextView)findViewById(R.id.textView2);
textView4 = (TextView)findViewById(R.id.textView4);
textView6 = (TextView)findViewById(R.id.textView6);
textView8 = (TextView)findViewById(R.id.textView8);
textView10 = (TextView)findViewById(R.id.textView10);
textView12 = (TextView)findViewById(R.id.textView12);
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d("onLocationChanged", location.getProvider() + " " + String.valueOf(location.getAccuracy()) + " " + location.getTime());
// GPSからの情報、前回の受信から10秒超、または前回の受信がNetwork
if(location.getProvider().equals(LocationManager.GPS_PROVIDER)
|| location.getTime() - lastUpdate > 10000
|| lastProvider.equals(LocationManager.NETWORK_PROVIDER)) {
textView2.setText(location.getProvider());
textView4.setText(String.valueOf(location.getLatitude()));
textView6.setText(String.valueOf(location.getLongitude()));
textView8.setText(String.valueOf(location.getAltitude()));
textView10.setText(String.valueOf(location.getAccuracy()));
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
textView12.setText(df.format(location.getTime()));
lastUpdate = location.getTime();
lastProvider = location.getProvider();
}
}
@Override
public void onProviderDisabled(String provider) {
// 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
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
locationManager.removeUpdates(this);
}
}
GPS、及び基地局からの情報の更新間隔は、requestLocationUpdatesメソッドのminTime引数やminDistance引数である程度調整が可能である。これで調整しきれない分はonLocationChangedイベントの中で自前で調整することになる。 onDestroyイベントではremoveUpdatesメソッドでGPSからの情報の停止要求を行う。onDestroyイベントはActivityが破棄される前に呼ばれる。 (2012/01/27)
Copyright(C) 2004-2013 モバイル開発系(K) All rights reserved.
[Home]
|
|||||