I've tried some ways to get current user location (latitude, langitude,..) but there are no any result. Can you help me? My last try with null location:
private void getLocation() throws IOException {
if (ActivityCompat.checkSelfPermission(MainWindow.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(MainWindow.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location locationNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location locationPassive = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (locationGPS != null)
setCoordinate(locationGPS.getLatitude(), locationGPS.getLongitude());
else if (locationNetwork != null)
setCoordinate(locationNetwork.getLatitude(), locationNetwork.getLongitude());
else if (locationPassive != null)
setCoordinate(locationPassive.getLatitude(), locationPassive.getLongitude());
else
Toast.makeText(this, "Ошибка", Toast.LENGTH_SHORT).show();
}
}
private void setCoordinate(double Latitude, double Longitude) throws IOException {
latitude = String.valueOf(Latitude);
longitude = String.valueOf(Longitude);
coordinate = "Широта: " + latitude + ", долгота: " + longitude;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
String state = addresses.get(0).getAdminArea();
}
Try to use fused location provider - https://developers.google.com/location-context/fused-location-provider .
It works really better.
That code worked for me:
public void showOurLocation() {
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((Activity) this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, 0);
return;
}
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
lastLocation = location;
String timestamp = new java.util.Date().toString();
String address = "";
if (Geocoder.isPresent()) {
try {
adr = (ArrayList<Address>) geocoder
.getFromLocation(location.getLatitude(), location.getLongitude(), 2);
address = adr.get(0).toString().split("\"")[1];
lastAddress = "Текущий адрес:\n" + address;
} catch (IOException e) {
e.printStackTrace();
}
}
LatLng weAreHere = new LatLng(location.getLatitude(), location.getLongitude());
lastLocationRecord=new LocationRecord(Calendar.
getInstance().getTime(),currentUserClearEmail,location,currentUser.getUid());
}
}
});
}
sometimes we still can get null, thats normal, I think, as long as we can get location sligtly later
Oh, you just should use listener for that in your code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.