java - getUIsettings.setMyLocationButtonEnabled(true) 不工作

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

我遇到了一个问题,我的位置按钮不会出现在右上角。我为 map 尝试了不同的 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();
            }
        }
    }
}

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

最佳答案

mMap.setMyLocationEnabled(true);

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

关于java - getUIsettings.setMyLocationButtonEnabled(true) 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53560760/

相关文章:

java - 如何使用 JPA 和 Hibernate 使用映射为 ORDINAL 的枚举参数进行查询

java - 为什么 rt.jar 不是类路径系统属性的一部分?

java - 为什么测试中的某些硬编码值会影响其他值?

android - cp : copyFileSync: could not write to dest file (code=ENOENT)

android - getView() 方法、CheckBox 和 setOnClickListener()

android - Fragment 中的参数仍然为空

android - 在 Viewpager 中使用具有相同 ViewModel 的相同 Fragment 的多个实例时出现问题

java - 在 Android 中修改外部库的最佳实践是什么?

javascript - Controller 无法在 ajax 响应中返回 JSON 对象结果

java - 安卓.view.InflateException : Binary XML file line #33: Error inflating class fragment