Android 位置(横向、纵向)未在行驶模式下更新
假设我从一个城市旅行到另一个城市。当用户按下按钮查看其位置应用程序显示其先前位置或错误位置时,在公交车内。但几分钟后,如果用户停留在那里,应用程序会显示正确的位置Android 位置(横向、纵向)未在行驶模式下更新,android,geolocation,location,Android,Geolocation,Location,假设我从一个城市旅行到另一个城市。当用户按下按钮查看其位置应用程序显示其先前位置或错误位置时,在公交车内。但几分钟后,如果用户停留在那里,应用程序会显示正确的位置 public void CurrentLocation() { mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity()); mLocationCallback = new LocationC
public void CurrentLocation() {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
}
};
LocationRequest mLocationRequest = new LocationRequest();
// mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
//mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
try {
mFusedLocationClient
.getLastLocation()
.addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful() && task.getResult() != null) {
mLocation = task.getResult();
Log.d(TAG, "Location : " + mLocation);
try {
Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String zip = addresses.get(0).getPostalCode();
String country = addresses.get(0).getCountryName();
String locality = addresses.get(0).getLocality();
String SubLocality = addresses.get(0).getSubLocality();
String SubAdminArea = addresses.get(0).getSubAdminArea();
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.w(TAG, "Failed to get location.");
}
}
});
} catch (SecurityException unlikely) {
Log.e(TAG, "Lost location permission." + unlikely);
}
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, null);
} catch (SecurityException unlikely) {
Log.e(TAG, "Lost location permission. Could not request updates. " + unlikely);
}
}
protected void onResume() {
super.onResume();
if (checkLocationPermission()) {
startLocationUpdates();
}
}
private void startLocationUpdates() {
CurrentLocation("");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback,
Looper.getMainLooper());
}