【发布时间】:2018-06-08 15:20:32
【问题描述】:
当我查看 Android 教程和/或 Android 官方文档时,似乎有多种不同的方式来查询位置。我很困惑,因为我不确定哪种方法是正确的,或者文档是否已过时。
例如,
1)GoogleApiClient:这样就使用了Google API客户端
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
然后它会像这样查询位置
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
2) 位置管理器:这种方式使用位置管理器
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
3) FusedLocationApi(第 2 种样式):
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations, this can be null.
if (location != null) {
// Logic to handle location object
}
}
});
我们应该使用哪种方式?
【问题讨论】:
标签: android location locationmanager android-fusedlocation