0

I am trying to learn using Permissions in Android and trying to use the method

mMap.setMyLocationEnabled(true);

which it says requires checkPermission. The following is the code i am writing:

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
//        LatLng sydney = new LatLng(-34, 151);
//        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
//        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_DENIED)
        {

            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MAPS_CODE);
        }
        else
        {
            mMap.setMyLocationEnabled(true);

        }
        }



    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {

        switch(requestCode)
        {
            case MAPS_CODE:
            {
                if(grantResults.length==0 || grantResults[0]==PackageManager.PERMISSION_DENIED)
                    Toast.makeText(this,"Permission denied",Toast.LENGTH_LONG).show();
                else
                    mMap.setMyLocationEnabled(true);
            }
        }
    }
}

However i am not able to use method

mMap.setMyLocationEnabled(true)

in onRequestPermissionsResult. It again asks for checkPermission check. What is the right way to write code for permissions. Kindly update

lospejos
  • 1,976
  • 3
  • 19
  • 35
user2779311
  • 1,688
  • 4
  • 23
  • 31

0 Answers0