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