gpt4 book ai didi

java - getUIsettings.setMyLocationButtonEnabled(true) 不工作

转载 作者:行者123 更新时间:2023-11-30 05:12:02 24 4
gpt4 key购买 nike

我遇到了一个问题,我的位置按钮不会出现在右上角。我为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com