setMyLocationEnabled() error on Android 6

android check location permission programmatically
android location permission
access_fine_location vs access_coarse_location
my location requires permission access_fine_location or access_coarse_location
get current location google maps api android
google maps permissions android
onmylocationclick
cannot enable mylocation layer as location permissions are not granted

I'm trying to make that when I load my map activity, a "my location" button will appear. This is my code, and the button which makes my activity to zoom in to my current location isn't appearing. What do I need to change in my code to make it work?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private static final String FIREBASE_URL="https://********.firebaseio.com/";
private Firebase firebaseRef;
private LocationManager locationManager;
private GoogleMap mMap;
SupportMapFragment mapFrag;
boolean bPermissionGranted;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps2);
    Firebase.setAndroidContext(this);
    firebaseRef = new Firebase(FIREBASE_URL);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        bPermissionGranted = checkLocationPermission();
    }
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);





    }
private void setUpMapIfNeeded() {
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

        if (mMap != null) {


            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(32.065483, 34.824550));
            CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);
            mMap.moveCamera(center);
            mMap.animateCamera(zoom);

        }
    }
}

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public boolean checkLocationPermission(){
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
            //  TODO: Prompt with explanation!

            //Prompt the user once explanation has been shown
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);

        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        return false;
    } else {
        return true;
    }
}



 @Override
 public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay!
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
                        ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    mMap.setMyLocationEnabled(true);
                }
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
            }
            return;
        }

    }
}


@Override
public void onMapReady(final GoogleMap googleMap) {

    mMap=googleMap;

    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (bPermissionGranted) {
            //User has previously accepted this permission
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                mMap.setMyLocationEnabled(true);
            }
        }
    }
    else {
        //Not in api-23, no need to prompt
        mMap.setMyLocationEnabled(true);
    }

    firebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot child : dataSnapshot.child("users").getChildren()) {


                String rightLocation = child.child("location_right").getValue().toString();
                String leftLocation = child.child("location_left").getValue().toString();

                double location_left = Double.parseDouble(leftLocation);
                double location_right = Double.parseDouble(rightLocation);
                String party_title = child.child("party/party_title").getValue().toString();
                LatLng cod = new LatLng(location_left, location_right);
                googleMap.addMarker(new MarkerOptions().position(cod).title(party_title));


            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
}
}

This is a simplified version of my other answer that also requests location updates .

The key is just to make sure that the user has granted you permission before you make the call to setMyLocationEnabled().

It's also possible that the user has not accepted the permission prompt by the time onMapReady() is called (most likely on first launch of the app), therefore there's another call to setMyLocationEnabled() in the onRequestPermissionsResult() callback in the case that the user accepted the permission.

First, ensure that you have the permissions in your AndroidManifest.xml (outside of the application tag):

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Here is a full Activity class that will work for you:

public class MapLocationActivity extends AppCompatActivity
        implements OnMapReadyCallback {

    GoogleMap mGoogleMap;
    SupportMapFragment mapFrag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            checkLocationPermission();
        }

        mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFrag.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //User has previously accepted this permission
            if (ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                mGoogleMap.setMyLocationEnabled(true);
            }
        } else {
            //Not in api-23, no need to prompt
            mGoogleMap.setMyLocationEnabled(true);
        }
    }

    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

    public boolean checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)) {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                //  TODO: Prompt with explanation!

                //Prompt the user once explanation has been shown
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);

            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);
            }
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay!
                    if (ActivityCompat.checkSelfPermission(this,
                            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        mGoogleMap.setMyLocationEnabled(true);
                    }
                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
                }
                return;
            }

        }
    }
}

If you revoke the permission:

It will prompt again:

Android 6.0: setMyLocationEnabled not work, This page provides Java code examples for com.google.android.gms.maps. how to use setMyLocationEnabled() of the com.google.android.gms.maps. Project: FiveMinsMore File: MapsActivity.java Source Code and License, Vote up, 6  The following are Jave code examples for showing how to use setMyLocationEnabled() of the com.google.android.gms.maps.GoogleMap class. You can vote up the examples you like.

You are using the same condition in if and else both blocks.

 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) 

So, if your above condition is not true then how will be the same condition gets true in your else block. That's the reason mMap.setMyLocationEnabled(true); never gets executed because your condition is false.

I guess what you want to do in else block is to request for permissions if it is not there. So it will change like this -

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    mMap.setMyLocationEnabled(true);
   } else{

       ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, <request_code>);
    }

And implement onRequestPermissionsResult callback

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case <request_code>:
                if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                     mMap.setMyLocationEnabled(true);
                }
                break; 
        }
    }

Location Data | Maps SDK for Android, I'm trying to use location layer to show user location in the map because '​setMyLocationEnabled' is deprecated. forceLocationUpdate() and see if the layer shows up. Location Layer 0.5.0 forceLocationUpdate not working #462 i'm running on Android 6.0.1, i have tested on android 5 and 6 before  Android 6.0 (Marshmallow) introduces a new model for handling permissions, which streamlines the process for users when they install and upgrade apps. If your app targets API level 23 or later, you can use the new permissions model.

Please write N instead of M after Build.VERSION_CODES.

In this line in your code:

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)

it worked for me

Java Code Examples com.google.android.gms.maps.GoogleMap , setMyLocationEnabled() error on Android 6 setAndroidContext(this); firebaseRef = new Firebase(FIREBASE_URL); if (android.os.Build. Android 런타임 권한에 대한 더 많은 코드 샘플 및 권장사항을 보려면 Android 권한 모델 문서를 참고하세요. 내 위치 레이어 지도에 내 위치 레이어를 사용 설정하려면 mMap.setMyLocationEnabled() 를 호출하세요.

"My location" not shown on map · Issue #451 · mapbox/mapbox , android.permission. <uses-permission android:name="android.permission. setMyLocationEnabled(true); Display an error message. public boolean onMyLocationButtonClick() { news (3) · mVoIP (1) · HTML5 (4) · JavaScript (7) · ORACLE (6) · Interview (0) · 인적성검사 (5) · CSS3 (0) · 여행계획 (0)  Android Google Maps V2 - MapView in XML. GitHub Gist: instantly share code, notes, and snippets.

180911(화) - Google Maps (Location data), You are reading a sample chapter from the Android Studio 1.x / Android 6 and calling the setMyLocationEnabled() method of that instance, passing through a  This is the main class of the Google Maps SDK for Android and is the entry point for all methods related to the map. You cannot instantiate a GoogleMap object directly, rather, you must obtain one from the getMapAsync () method on a MapFragment or MapView that you have added to your application. Note: Similar to a View object, a GoogleMap

Working with the Google Maps Android 6 API in Android Studio , In this Google Maps API Tutorial for Android you will learn how to retrieve the user's current Starting with Android 6.0, user permissions are handled a little differently than before. Add the following inside setUpMap() just below the setMyLocationEnabled() call: SendIntentException) { // Ignore the error. }  in google map if i open there is a function to set my location button enable and disable "gmaps.getUiSettings().setMyLocationButtonEnabled(false);" same way is there any function in mapbox sdk to enable or disable? or we need to write ou

Comments
  • I've tried your answer, but there is an error message on every time this line is written: mMap.setMyLocationEnabled(true);. The error says: call requires permission which may be rejected by user: code should explictly check if permission is available (with 'checkPermission') or explictly handle a potential 'SecurityExpection'
  • And its even not asking me a permission to use my locations in my phone.
  • @olash12345 I just updated the answer again to simplify it even more. With the added checks necessary, there was no need for the boolean. I just tested this on both a Nexus 5 with 6.0.1, and a Nexus 6 with 6.0.1, and it prompts for the Location permission, and then enables the MyLocaction button, which works as expected.
  • It is not asking me a permission. And also it is not showing the button. Do I have to call the function onMapReady() somewhere?
  • @olash. No, onMapReady() gets called after you call getMapAsync(). Not sure why it's not working for you, it works just fine for me. Check your logs for possible errors.
  • Hey, I've tried your answer, and there are two errors appearing: 1) What are you trying to do in this line case <requestCode>:? It causes an error. 2) This line shows an error: mMap.setMyLocationEnabled(true); which says: call requires permission which may be rejected by user: code should explictly check if permission is available (with 'checkPermission') or explictly handle a potential 'SecurityExpection'