getUIsettings.setMyLocationButtonEnabled(true)不起作用

时间:2018-11-30 15:49:52

标签: java android android-fragments google-maps-android-api-2

我遇到了一个问题,我的位置按钮无法显示在右上角。我尝试了地图的其他UI设置,例如放大和缩小,这些设置会出现。我没有这个问题的主意。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.activity_maps, container, false);
    // TODO: get rid of commented out code
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
            .findFragmentById(R.id.maps);
    mapFragment.getMapAsync(this);
    getLocationPermission();
    return view;
}


/**
 * 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) {

    mMap = googleMap;

    if (mLocationPermissionsGranted) {
        getDeviceLocation();

        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(),
                Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(true);
            mMap.getUiSettings().setZoomControlsEnabled(true);
            return;
        }
    }
}

private void getDeviceLocation(){
    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getContext());

    try{
        if(mLocationPermissionsGranted){

            final Task location = mFusedLocationProviderClient.getLastLocation();
            location.addOnCompleteListener(new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    if(task.isSuccessful()){
                        Location currentLocation = (Location) task.getResult();
                    }else{
                        Toast.makeText(getContext(), "ERROR LOCATION", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }catch (SecurityException e){ }
}

private void initMap(){
    SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
            .findFragmentById(R.id.maps);
    mapFragment.getMapAsync(this);
}

private void getLocationPermission(){
    String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION};

    if(ContextCompat.checkSelfPermission(getContext(),
            FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
        if(ContextCompat.checkSelfPermission(getContext(),
                COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
            mLocationPermissionsGranted = true;
            initMap();
        }else{
            ActivityCompat.requestPermissions(getActivity(),
                    permissions,
                    LOCATION_PERMISSION_REQUEST_CODE);
        }
    }else{
        ActivityCompat.requestPermissions(getActivity(),
                permissions,
                LOCATION_PERMISSION_REQUEST_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    mLocationPermissionsGranted = false;

    switch(requestCode){
        case LOCATION_PERMISSION_REQUEST_CODE:{
            if(grantResults.length > 0){
                for(int i = 0; i < grantResults.length; i++){
                    if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
                        mLocationPermissionsGranted = false;
                        return;
                    }
                }
                mLocationPermissionsGranted = true;
                //initialize our map
                initMap();
            }
        }
    }
}

我确实拥有所有许可。我认为问题可能与权限的完成方式有关。因为每当弹出权限访问权限时,我都必须在接受后退出片段并返回以查看我的位置。

1 个答案:

答案 0 :(得分:0)

mMap.setMyLocationEnabled(true);

直接寻址地图对象,而没有mMap.getUiSettings()为我修复它。