ildar ishalin
ildar ishalin

Reputation: 755

setMyLocationEnabled on Android 6 Marshmallow (map permissions)

With new runtime updates, you must ask for permission every time you use something personal.

In google maps setMyLocationEnabled can't be provided if user denied map permissions.

But in Google Maps app itself this button is available and permission is asked when user clicks it. Basically, in my app, if user denied map permissions, I have to draw the map without that button, but I want to make it available with permission check on click. How can I do that?

Edit: setMyLocationButtonEnabled was of any help. MyLocation button does not appear even though isMyLocationButtonEnabled returns true.

Upvotes: 3

Views: 1563

Answers (1)

Carlos Gómez
Carlos Gómez

Reputation: 357

try this:

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private static final int REQUEST_ACCESS_FINE_LOCATION = 1;
    private static String[] PERMISSIONS_MAPS = {
                    Manifest.permission.ACCESS_FINE_LOCATION};
    private GoogleMap _googleMaps;

    //region Override
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // 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);
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        _googleMaps = googleMap;
        UiSettings settings = _googleMaps.getUiSettings();
        settings.setZoomControlsEnabled(true);
        // Enabling MyLocation Layer of Google Map
        if (checkPermission()) {
            try {
                _googleMaps.setMyLocationEnabled(true);
            }
            catch (Exception e){
            }
        }
        else{
            verifyMapsPermissions(this);
        }
        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        _googleMaps.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        _googleMaps.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch ( requestCode ) {
            case REQUEST_ACCESS_FINE_LOCATION: {
                if (grantResults.length > 0
                                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission granted
                    if (checkPermission())
                        _googleMaps.setMyLocationEnabled(true);

                }
                break;
            }
        }
    }

    //endregion

    //region Permisos
    private boolean checkPermission() {
        // Ask for permission if it wasn't granted yet
        return (ContextCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED );
    }

    public static void verifyMapsPermissions(FragmentActivity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                            activity,
                            PERMISSIONS_MAPS,
                            REQUEST_ACCESS_FINE_LOCATION
            );
        }
    }
    //endregion
}

if you use AppCompactActivity just change

public static void verifyMapsPermissions(FragmentActivity activity) {...

for

public static void verifyMapsPermissions(AppCompatActivity activity) {...

Upvotes: 0

Related Questions