我正在开发跟踪用户实时位置的应用程序,并且我正在使用 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();
}
}