我正在尝试使用FusedLocationApi获取用户当前位置,即使没有wifi或移动网络也是如此。
如何使用融合的位置API实现此目标??
位置管理器具有此功能,我不想像在保险丝位置中那样使用位置管理器,因此我得到了最准确的结果,如何实现这一点,所以让我发表到目前为止的尝试
请参阅下面的代码
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
mGoogleApiClient.connect();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setSmallestDisplacement(300);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
PendingResult<Status> statusPendingResult =
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, new LocationCallback() {
@Override
public void onLocationResult(LocationResult result) {
Log.d("TAG", "onLocationResult" + result.getLastLocation().getLatitude() + " " + result.getLastLocation().getLongitude());
}
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
Log.d("TAG", "onLocationAvailability: isLocationAvailable = " + locationAvailability.isLocationAvailable());
}
}, null);
statusPendingResult.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.getStatus().isSuccess()) {
Log.d("TAG", "Successfully requested location updates");
} else {
CommonMethods.setAddress("Couldn't find the location");
Toast.makeText(MainActivity.this, "Couldn't get the location. Make sure location is enabled on the device", Toast.LENGTH_SHORT).show();
Log.e("TAG",
"Failed in requesting location updates, "
+ "status code: "
+ status.getStatusCode() + ", message: " + status
.getStatusMessage());
}
}
});
答案 0 :(得分:0)
通过添加此行
mLocationRequest.setSmallestDisplacement(300);
您意识到两个位置之间的差异距离应该为300米,您是否试图覆盖那么远的距离?
尝试将其删除