Trouble catching latitude and longitude

Asked

Viewed 143 times

0

My code was working, I was getting latitude and longitude normally, but it stopped and I’m not getting the location anymore. It only falls on the condition that it was not possible to pick up the location

  private void foto(int actionCode) {

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    final boolean GPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (GPSEnabled) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        switch (actionCode) {
            case ACTION_TAKE_PHOTO_B:
                File f = null;
                try {
                    f = setUpPhotoFile();
                    mCurrentPhotoPath = f.getAbsolutePath();
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                    if (ActivityCompat.checkSelfPermission(this,
                            android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    } else {
                        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    }
                    if (location != null) {
                        longitude = location.getLongitude();
                        latitude = location.getLatitude();
                    }
                    else {
                        Toast.makeText(Menu.this, "Não foi possivel obter sua localização" +
                                "", Toast.LENGTH_SHORT).show();
                        return;
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                    f = null;
                    mCurrentPhotoPath = null;

                }
                break;

            default:
                break;
        } // switch
  • Puts a Log.d within this if: if (ActivityCompat.checkSelfPermission(this,
 android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Log.i("MINHA_TAG", "Sem permissão"); }

  • I did, but it doesn’t go through that log

  • getLastKnownLocation actually returns the last registered position, maybe he could not register his last position, open google maps ask to locate you and then open your app just to test.

  • I did it and it didn’t work, but I have a GPS Status app, when I open it and it picks up location, I open my app, but when saved in the bank is only in the same location, no matter where I go.

  • But why is the question bank or GPS? Okay, let’s split, first look at this https://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener) (android.com link breaks, copies and pastes in the browser)

1 answer

0

The problem is that you’re just checking for permission, but not asking for permission to be granted. Below follows a method that checks and already grants permission using requestPermissions() through user interaction:

public boolean getPermissionLocalization(Context context) {
        int REQUEST_PERMISSION_LOCALIZATION = 221;
        boolean res = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    public void requestPermissions(@NonNull String[] permissions, int requestCode)
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for Activity#requestPermissions for more details.

                res = false;
                ActivityCompat.requestPermissions((Activity) context, new String[]{
                                Manifest.permission.ACCESS_FINE_LOCATION},
                        REQUEST_PERMISSION_LOCALIZATION);

            }
        }
        return res;
    }

There before you request, make the following check:

if(getPermissionLocalization(this)){
    // permissão concedida
} else {
    // permissão não concedida
}

Behold that answer for more details.

  • On my phone it is not going through that if that requests the location permission. I have the premises in manifest tbm

  • @Julianomorche Esse if I just created there in the reply. I’m telling you to adapt to your code. You tried to do this?

  • Good , I put as you said, and he falls into the granted option

Browser other questions tagged

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