Problem catching latitude and longitude android 5

Asked

Viewed 120 times

2

My code does not get location on android 5,

 public void pegaLocalizacao() {
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    latitude =  location.getLatitude();
    longitude = location.getLongitude();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    }
    locListener = new LocationListener() {
        public void onLocationChanged(Location location) {


        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }

    };

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 30000, 0, locListener);


}
  • Did you add permissions in Manifest? What is the error shown?

  • Could you mention what exactly happens? Error? any Log? It goes through the method onLocationChanged ???

2 answers

1

Try it like this:

private void checkPermission() {
    // Verifica necessidade de verificacao de permissao
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // Verifica necessidade de explicar necessidade da permissao
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
            Toast.makeText(this, "E necessario permitir o uso da localização", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    CODE_PERMISSION_LOCATION);
        } else {
            // Solicita permissao
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    CODE_PERMISSION_LOCATION);
        }
    }
}

1

Try this way:

 public void pegaLocalizacao() {

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.
            try {
                List<Address> geocodeMatches = new Geocoder(MainActivity.this).getFromLocation(location.getLatitude(), location.getLongitude(),1);
                geocodeMatches.get(0);

                double latidute = location.getLatitude();
                double longitude = location.getLongitude();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    } else {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
}
  • Thank you Arthur, it worked right in mine, because even in my android 4 stopped working overnight. I just don’t understand what that code you put in the most does, if you can give me a brief explanation.

  • Good Julian, face in real put the first two lines of Try, because in the code here I use to search location data, as address, name of the city, etc. But for your case can disregard them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.