如果用户不允许Android中地图的精细位置权限,则添加默认位置 [英] Adding Default Location if User Doesn't Allow Permission for a Fine Location for a Map in Android

查看:287
本文介绍了如果用户不允许Android中地图的精细位置权限,则添加默认位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习运行时权限.我有一张地图,并且获得了我的许可.如果用户允许精确的位置-地图会缩放到他的当前位置,如果他不允许,则地图会保留在该位置.我想做的而不是将地图留在原处是这样的: 如果用户不允许访问其位置的权限,则地图应放大到我的自定义位置 (格林威治).

I am starting to learn about the runtime permissions. I have a map and I made a permission. If user allows fine location - map zooms to his current location, and if he doesn't allow it, the map stays in the place. What I want to do instead of map staying in the place is this: if the user doesn't allow permission for accessing his location, map should zoom in to my custom location (Greenwich).

我的地图片段:

        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) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {

                } else {
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_LOCATION_REQUEST_CODE);
                }
                return;
            }

            mMap.setMyLocationEnabled(true);
            LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
            Criteria criteria = new Criteria();

//                if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION)
//                        == PackageManager.PERMISSION_GRANTED) {
//                    mMap.setMyLocationEnabled(true);
//                } else {
//                    // Show rationale and request permission.
//                }

            //Disable Map Toolbar:
            mMap.getUiSettings().setMapToolbarEnabled(false);

            // Get the name of the best provider and current location
            String provider = null;
            if (locationManager != null) {
                provider = locationManager.getBestProvider(criteria, true);
            }
            // Get current location
            Location myLocation = null;
            if (locationManager != null) {
                myLocation = locationManager.getLastKnownLocation(provider);
            }

            // Set default latitude and longitude to Greenwich
            double latitude = 51.4825766;
            double longitude = -0.0076589;

            // Get latitude and longitude of the current location and a LatLng object
            if (myLocation != null) {
                latitude = myLocation.getLatitude();
                longitude = myLocation.getLongitude();
            }

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(latitude, longitude)).zoom(14).build();
            mMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));

}

推荐答案

找不到解决方案,因此我将权限放入了父活动中.我认为这是一个已知的错误.

Couldn't find solution so I put the permission inside the parent activity. I think it is a known bug.

这篇关于如果用户不允许Android中地图的精细位置权限,则添加默认位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆