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();
}