java - 我在 android studio 中的位置有问题

标签 java android google-maps android-maps-v2

大家晚上好,我想告诉你我的问题,所以我希望在我的 android 中的谷歌地图中出现转到我当前位置的按钮,但是当我在我的真实手机中尝试它时(三星 SM-J730G android 7.0 api 24) 小按钮没有出现(我没有在模拟器中尝试过,因为我也不起作用)。所以我想知道问题出在哪里请 friend 们帮助我非常感谢您的帮助是代码:

public class MapFragment extends Fragment implements View.OnClickListener {


private View rootView;
private MapView mapView;
private GoogleMap mMap;
private FloatingActionButton fab;


public MapFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    this.rootView = inflater.inflate(R.layout.fragment_map, container, false);
    this.fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
    return rootView;
}


 @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    this.fab.setOnClickListener(this);
    this.mapView = (MapView) this.rootView.findViewById(R.id.map);
    if (mapView != null) {
        this.mapView.onCreate(null);
        this.mapView.onResume();
        this.mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;
                if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                mMap.setMyLocationEnabled(true); //I DONT KNOW WHY IT DOES NOT WORK


                  }
              });

    }



    super.onViewCreated(view, savedInstanceState);
}

哦,顺便说一句,我调试了它

 if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;   //is entrying here and i donw know why if i have the perssions in the manifest the accces_fine_location and acces_coarse_location
                }
                mMap.setMyLocationEnabled(true);   


please somebody help me thanks you

最佳答案

你只检查了运行时权限,如果没有提供你没有请求权限。更新代码如下和onRequestPermissionsResult检查是否提供权限设置mMap.setMyLocationEnabled(真); 从那里

     @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        this.fab.setOnClickListener(this);
        this.mapView = (MapView) this.rootView.findViewById(R.id.map);
        if (mapView != null) {
           his.mapView.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap googleMap) {
                    mMap = googleMap;
                    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

  ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
                        return;
                    }
                    mMap.setMyLocationEnabled(true); //I DONT KNOW WHY IT DOES NOT WORK


                      }
                  });

        }

关于权限结果

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 101) {
  f (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);  }
  }                 
}

关于java - 我在 android studio 中的位置有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52378399/

相关文章:

android - 从android中的数据库获取经纬度值

google-maps - 在哪里可以找到适用于 Python 的 Google Places API 客户端库?

iphone - 将图 block 坐标转换为像素再转换为地理坐标

java - JSON jsonObject.optString() 返回字符串 "null"

java - 使用 JNI 通过 std::ostream 从 C++ 获取文本数据到 Java

java - 如何创建 IF 语句来检查值是否最接近?

Android - 横向覆盖的导航栏

android - 什么时候需要 BLUETOOTH_ADMIN android 权限

swift - 我无法将当前位置添加到 Google map

java - 当特定类成员在发布版本之间更改值时破坏 Maven 构建的良好实践