Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[android][GPS]LocationManager
// copy from http://goo.gl/NxT8B
private LocationManager mLocationManager;
private String mBestProvider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 位置情報サービスマネージャを取得
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// 位置情報サービスの要求条件をピックアップする
// 速度、電力消費などから適切な位置情報サービスを選択する
Criteria criteria = new Criteria();
// 使える中で最も条件にヒットする位置情報サービスを取得する
mBestProvider = mLocationManager.getBestProvider(criteria, true);
}
@Override
public void onPause()
{
if (mLocationManager != null) {
mLocationManager.removeUpdates(mLocationListener);
}
super.onPause();
}
@Override
protected void onResume() {
if (mLocationManager != null) {
// 以前に取得した位置情報を取得
Location location = mLocationManager.getLastKnownLocation(mBestProvider);
// 位置更新の際のリスナーを登録。
// 省電力のために通知の制限。
// 60000ms周期、100mの位置変化の場合
mLocationManager.requestLocationUpdates(mBestProvider, 60000, 100, mLocationListener);
}
super.onResume();
}
final LocationListener mLocationListener = new LocationListener() {
// LocationListener Override Method
@Override
public void onLocationChanged(Location location) {
Log.v("----------", "----------");
Log.v("Latitude", String.valueOf(location.getLatitude()));
Log.v("Longitude", String.valueOf(location.getLongitude()));
Log.v("Accuracy", String.valueOf(location.getAccuracy()));
Log.v("Altitude", String.valueOf(location.getAltitude()));
Log.v("Time", String.valueOf(location.getTime()));
Log.v("Speed", String.valueOf(location.getSpeed()));
Log.v("Bearing", String.valueOf(location.getBearing()));
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
Log.v("Status", "AVAILABLE");
break;
case LocationProvider.OUT_OF_SERVICE:
Log.v("Status", "OUT_OF_SERVICE");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v("Status", "TEMPORARILY_UNAVAILABLE");
break;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment