0

I have a map fragment. In the map fragment, I am using a FAB button for getting user's current location and showing it on the map. I have declared the following permissions in the Manifest file. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Now there are 2 cases:

(1) The user's location was already turned on: In this case when I press location FAB button, it animates the map to users current location and shows the blue dot on map representing user's current location. This is working as expected

(2) The user's location was off: Now this is where I have problem. I want the dialog box to appear, when the user clicks on the location FAB , requesting user to turn on his location & if location granted then move to users current location. But, when I press the FAB, nothing is happening. The following is the code for on setOnClickListener for the location FAB.

locationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("MapFragment", "Location button pressed" );
            try {
                Log.v("MapFragment", "Inside try" );

                if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    Log.v("MapFragment", "inside if 0.7" );
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);
                    Log.v("MapFragment", "inside if 1" );
                    return;
                }
                currentLocation = getLastKnownLocation();
                if (currentLocation != null) {
                    Log.v("MapFragment", "inside if 2" );
                    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), 17);
                    googleMap.animateCamera(cameraUpdate);
                    locationButton.getDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
                }


            } catch (Exception e){
                checkLocationPermission();
                Toast.makeText(getContext(), "Please turn on Location from the Settings", Toast.LENGTH_SHORT).show();

            }
        }
    });

the getLastKnownLocation() function is as follows:

private Location getLastKnownLocation() {
    //Create a location manager object instance
    LocationManager mLocationManager = (LocationManager) getContext().getSystemService(LOCATION_SERVICE);
    //Get all the different providers which can give current location info
    List<String> providers = mLocationManager.getProviders(true);
    //Initialising the location to be null
    Location bestLocation = null;
    //Creating a for loop to go through all the location providers and get the location
    for (String provider : providers) {

        //if permission is not granted, then get the permission

        if (ContextCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {


            ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        //Get the last known location from the data provider
        Location l = mLocationManager.getLastKnownLocation(provider);
        //If the last know location provided by the data provider is null then ignore the provider and move to the next one.
        if (l == null) {
            continue;
        }

        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            bestLocation = l;
        }
    }
    return bestLocation;
}

The log messages that I get on pressing FAB in case 2 are as follows

01-16 13:52:15.728 18227-18227/in.map.app V/MapFragment: Location button pressed
01-16 13:52:15.728 18227-18227/in.map.app V/MapFragment: Inside try
OzoneX
  • 93
  • 2
  • 14
  • 2
    You just need to check if location is turned on. Checking for permission isn't sufficient. Take a look at this https://stackoverflow.com/questions/10311834/how-to-check-if-location-services-are-enabled – Ranjan Jan 16 '18 at 09:36
  • The above comment is more helpful then answer given by Kushaal Singla. – OzoneX Jan 16 '18 at 10:17

1 Answers1

0

You need to override this method and accordingly to do next operation.

void onRequestPermissionsResult (int requestCode, 
                String[] permissions, 
                int[] grantResults)
KKSINGLA
  • 1,284
  • 2
  • 10
  • 22