public void getDeviceLocation() {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(getMvpView().getActivity());
settingsClient = LocationServices.getSettingsClient(getMvpView().getActivity());
createLocationCallback();
createLocationRequest();
buildLocationSettingsRequest();
startLocationUpdates();
}
private void createLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void createLocationCallback() {
getMvpView().showProgressDialog();
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Location currentLocation = locationResult.getLastLocation();
getMvpView().showSelectedAddress(getAddressFromLatLng(currentLocation.getLatitude(), currentLocation.getLongitude()));
fusedLocationClient.removeLocationUpdates(locationCallback);
getMvpView().hideProgressDialog();
}
};
}
private void buildLocationSettingsRequest() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
locationSettingsRequest = builder.build();
}
private void startLocationUpdates() {
settingsClient.checkLocationSettings(locationSettingsRequest)
.addOnSuccessListener(getMvpView().getActivity(), locationSettingsResponse -> {
if (ActivityCompat.checkSelfPermission(getMvpView().getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getMvpView().getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
fusedLocationClient.requestLocationUpdates(locationRequest,
locationCallback, Looper.myLooper());
}).addOnFailureListener(getMvpView().getActivity(), e -> {
getMvpView().hideProgressDialog();
getMvpView().showErrorToast(R.string.please_enable_location);
});
}
@SuppressLint("MissingPermission")
private void startLocationUpdates() {
settingsClient.checkLocationSettings(locationSettingsRequest)
.addOnSuccessListener(getMvpView().getActivity(), locationSettingsResponse -> {
fusedLocationClient.requestLocationUpdates(locationRequest,
locationCallback, Looper.myLooper());
}).addOnFailureListener(getMvpView().getActivity(), e -> {
getMvpView().hideProgressDialog();
getMvpView().showErrorToast(R.string.please_enable_location);
});
}
private String getAddressFromLatLng(double latitude, double longitude) {
Geocoder geocoder = new Geocoder(getMvpView().getActivity(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
latitude,
longitude,
1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addresses.get(0);
StringBuilder addressStringBuilder = new StringBuilder();
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
addressStringBuilder.append(address.getAddressLine(i));
}
return addressStringBuilder.toString();
}
我已经将纬度和经度转换为地址文本,但在我再次查询位置后,即使我没有移动我的当前位置,并在手机设置中启用了精度,地址也会发生更改。问题是,当我总是查询时,如果我的纬度和经度位置没有改变,它应该给我相同的位置地址。