在安卓6.0 Marshmallow上设置我的位置启用(地图权限)

3

在新的运行时更新中,每次使用个人信息时都必须请求权限。

如果用户拒绝地图权限,则无法提供Google Maps中的setMyLocationEnabled功能。

但是,在Google Maps应用程序本身中,此按钮可用,并且当用户单击它时会请求权限。基本上,在我的应用程序中,如果用户拒绝了地图权限,我必须在没有该按钮的情况下绘制地图,但我希望可以通过点击进行权限检查后使其可用。我该怎么做?

编辑:setMyLocationButtonEnabled没有任何帮助。即使isMyLocationButtonEnabled返回true,MyLocation按钮也不会出现。


你设置了setMyLocationEnabled和setMyLocationButtonEnabled为true吗? - ravidl
1
正如我所说,如果用户拒绝地图授权(安全异常),setMyLocationEnabled无法设置。 UiSettings的setMyLocationButtonEnabled也不起作用。 - ildar ishalin
1个回答

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) { ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接