java - 我在 android studio 中的位置有问题
问题描述
大家晚上好,我想告诉你我的问题,所以我希望在我的 android 的谷歌地图中出现指向我当前位置的按钮,但是当我在我的真实手机中尝试时(三星 SM-J730G android 7.0 api 24)没有出现小按钮(我没有在模拟器中尝试,因为我也不起作用)。所以我想知道是什么问题,请朋友在这里帮助我,非常感谢您的帮助是代码:
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(true);
@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); }
}
}
推荐阅读
- tensorflow - TensorFlow.js 无法在 Windows 10 中安装
- javascript - 为什么我无法从共享邮箱发送消息,即使删除了插件
- visual-studio-2015 - Visual Studio 2015 设计器不想显示表单 - 错误
- classification - 如何将数值类转换为 WEKA 中的因子?
- kubernetes - pod 网络错误
- java - 使用标志 CLEAR_TOP 时后台堆栈中的活动
- mongodb - 使用 Mongosheet 的 CRUD 操作
- facebook - Facebook Graph API -> 用户/事件返回空数组
- php - 我想从我的数据库中从一个 ID 号中选择多个具有相同 photo_id 的图像或变量
- python - 使用 Flask 从静态文件夹提供静态文件