ユーザーの所在地を取得するだけで済みます。好ましくは正確な場所ですが、それが不可能な場合は、大まかな場所が良いでしょう。ドキュメントによればLocationManagerとLocationClientを合わせてユーザーの場所を取得する
:
LocationClient.getLastLocation()
Returns the best most recent location currently available.
と
LocationManager.getLastKnownLocation(String)
Returns a Location indicating the data from the last known location fix obtained from the given provider.
私の理解が正しいとすれば、前者は私に非常に良い結果(または時にはヌル)を与えますが、後者はまれではない結果を私に与えるでしょう。
これは私のコードでは(簡体字)
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationClient = new LocationClient(this, this, this);
@Override
public void onConnected(Bundle dataBundle) {
setUserLocation();
}
private void setUserLocation() {
myLocation = locationClient.getLastLocation();
if (myLocation == null) {
myLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(new Criteria(), false));
if (myLocation == null) {
//I give up, just set the map somewhere and warn the user.
myLocation = new Location("");
myLocation.setLatitude(-22.624152);
myLocation.setLongitude(-44.385624);
Toast.makeText(this, R.string.location_not_found, Toast.LENGTH_LONG).show();
}
} else {
isMyLocationOK = true;
}
}
それが動作しているようだが、私の質問は以下のとおりです。
getLastLocation
とgetLastKnownLocation
正しいの私の理解ですか?- これは良いアプローチですか?
- 同じアクティビティで両方を使用して問題が発生する可能性はありますか?位置フィックスが決定することは不可能である場合
おかげ
お役立ち情報Google Playの可用性を処理するコードは既に用意されています(実際にはアプリがなくても機能しない可能性があります)。最初はLocationServicesだけを使用しましたが、何度も私はそこから奇妙な結果を得ました。 LocationClientに切り替えると、より良い結果が得られましたが、再現できなかった多くの場合nullが返されます。私はAndroidシステムがLocationServiceクラスにしかアクセスできない場所の最後の既知の場所を記録している可能性があると考えました。そうでない場合は、LocationClientだけに頼る方が良いでしょうか? – Androiderson
[公式の開発者向けWeb](http://developer.android.com/guide/topics/)の[flow](http://developer.android.com/images/location/getting-location.png)を参照してください。 location/strateg.html) – Aiapaec