0

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();
    }
hata
  • 11,633
  • 6
  • 46
  • 69
  • possibly duplicate https://stackoverflow.com/questions/19621882/getlastknownlocation-returning-null – etomun May 04 '20 at 12:04

2 Answers2

0

Try to use fused location provider - https://developers.google.com/location-context/fused-location-provider.

It works really better.

psa98
  • 1
  • 2
  • Code from https://developer.android.com/training/location/retrieve-current.html doesn't help. I paste this fusedLocationClient.getLastLocation() .addOnSuccessListener(this, new OnSuccessListener() { @Override public void onSuccess(Location location) { // Got last known location. In some rare situations this can be null. if (location != null) { // Logic to handle location object } } }); but still have problem. location = null – Захар Дементьев May 04 '20 at 12:29
0

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.

MMG
  • 3,226
  • 5
  • 16
  • 43
psa98
  • 1
  • 2