android - FusedLocationProviderClient 有时会返回相同的位置
问题描述
我正在开发跟踪用户实时位置的应用程序,并且我正在使用 FusedLocationProviderClient 获取当前位置,但有时它会返回相同的位置,即应用程序在打开应用程序时会获取一个位置,在移动到另一个地方后会在应用程序尝试时说不同的村庄要获得新位置,它会再次获得以前的位置......
请帮忙
我的代码如下。
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(this);
locationRequest = LocationRequest.create();
locationRequest.setInterval(MIN_UPDATE_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
protected void onResume() {
super.onResume();
try {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int result = googleApiAvailability.isGooglePlayServicesAvailable(this);
if (result != ConnectionResult.SUCCESS && result != ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED) {
Toast.makeText(this, "Are you running in Emulator ? try a real device.", Toast.LENGTH_SHORT).show();
}
callCurrentLocation(0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void callCurrentLocation(final int type) {
try {
if (
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
) {
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
requestPermissions(REQUEST_PERMISSIONS_CURRENT_LOCATION_REQUEST_CODE);
return;
}
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
currentLocation = (Location) locationResult.getLastLocation();
if (currentLocation != null) {
latitude = currentLocation.getLatitude();
longitude = currentLocation.getLongitude();
currentAccuracy = currentLocation.getAccuracy();
}
String result = "Current Location Latitude is " +
currentLocation.getLatitude() + "\n" +
"Current location Longitude is " + currentLocation.getLongitude() + " \n Accuracy is " + currentLocation.getAccuracy();
Toast.makeText(act, "current Location : " + result, Toast.LENGTH_SHORT).show();
}
};
mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.myLooper());
} catch (Exception ex) {
ex.printStackTrace();
}
}
解决方案
推荐阅读
- python - 有没有办法使用硒与网页中打开的外部应用程序警报进行交互?
- notepad++ - 使用 Notepad++ 在两个位置同时编辑两个相同的文件
- javascript - 如何在反应中使用formik附加输入
- python - 如何导入子包?
- html - Chrome 中日期类型输入元素上的清除按钮在哪里?
- flutter - 尽管发送了 Flutter StreamBuilder 的快照,但它没有日期——snapshot.data 始终设置为 initialData
- android - android webview仅显示空白页面特定的url
- datetime - Power Query - 将日期时间更改为文本而不更改格式
- reactjs - 在material-ui useStyles()中通过id访问元素
- spring-batch - 如何防止春季批处理启动新的作业实例?