LocationClient は非推奨です。次のように を使用する必要がありますGoogleApiclient
。
1: GoogleApiClient 変数を宣言する
private GoogleApiClient mGoogleApiClient;
2: インスタンス化
mGoogleApiClient = new GoogleApiClient.Builder(mThisActivity)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
3: コールバックを実装する
public class YourClass extends BaseFragment implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
@Override
public void onConnectionFailed(ConnectionResult result) {
// your code goes here
}
@Override
public void onConnected(Bundle connectionHint) {
//your code goes here
}
@Override
public void onConnectionSuspended(int cause) {
//your code goes here
}
}
4: 位置情報の更新を開始します。
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
5: 場所の更新を削除します。
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
6: 最後の既知の場所を取得する:
private Location mCurrentLocation;
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);