Android 6 Marshmallow上的setMyLocationEnabled(地图权限)

时间:2015-12-24 11:36:28

标签: android google-maps android-6.0-marshmallow android-permissions

使用新的运行时更新时,每次使用个人内容时都必须请求权限。

如果用户拒绝了地图权限,则无法在Google地图中提供setMyLocationEnabled

但是在Google地图应用中,此按钮可用,并在用户点击时询问权限。 基本上,在我的应用程序中,如果用户拒绝了地图权限,我必须在没有该按钮的情况下绘制地图,但我想通过单击权限检查使其可用。我怎么能这样做?

编辑:setMyLocationButtonEnabled有任何帮助。即使isMyLocationButtonEnabled返回true,也不会显示“我的位置”按钮。

1 个答案:

答案 0 :(得分:0)

试试这个:

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
}

如果您使用AppCompactActivity,只需更改

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

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