【发布时间】:2016-12-13 07:43:58
【问题描述】:
我已经在我的应用中实现了 Google Play 服务的 Fused Location Api,以不断获取用户位置。 wifi开启时一切正常
但是当我关闭 wifi 时,它会使用蜂窝信息更新用户位置,因此准确度超过 700 米。
我想要的是当 wifi 关闭时,然后从 GPS 获取用户位置。
protected synchronized void buildGoogleApiClient() {
Log.i(TAG, "Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "Connected to GoogleApiClient");
if (mCurrentLocation == null) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
updateUI();
}
if (mRequestingLocationUpdates) {
startLocationUpdates();
}
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
updateUI();
Toast.makeText(this, getResources().getString(R.string.location_updated_message),
Toast.LENGTH_SHORT).show();
}
【问题讨论】:
-
将此添加到您的 mLocationRequest
mLocationRequest.setSmallestDisplacement(5); -
这只会设置 5m 位移进行更新。
-
这将提高您的准确性,因为如果用户位置变化超过 5 米,它将触发位置更新
标签: android android-gps android-fusedlocation