简体   繁体   English

如果用户不允许Android中地图的精细位置权限,则添加默认位置

[英]Adding Default Location if User Doesn't Allow Permission for a Fine Location for a Map in 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). 我想做的而不是将地图留在原处是这样的: 如果用户不允许访问其位置的权限,则地图应放大到我的自定义位置 (格林威治)。

My maps fragment: 我的地图片段:

        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));

}

You can zoom to location manually with this code : 您可以使用以下代码手动缩放到位置:

CameraUpdate center=CameraUpdateFactory.newLatLng(new LatLng(latitude,
longitude));

CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

map.moveCamera(center);
map.animateCamera(zoom);

Couldn't find solution so I put the permission inside the parent activity. 找不到解决方案,所以我将权限放入父活动中。 I think it is a known bug. 我认为这是一个已知的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Android中获得ACCESS_FINE_LOCATION权限 - Can't get ACCESS_FINE_LOCATION permission in android 拒绝在地图上的精细位置权限转到自定义位置 - On Map Fine Location Permission Denied Go To Custom Location 尝试在Android Studio中请求具有位置权限的对话框,但它不起作用 - Trying to request a dialog with the location permission in Android Studio but it doesn't work WebChromeClient 不再请求位置权限 - WebChromeClient doesn't ask for location permission anymore Android权限ACCESS_FINE_LOCATION始终被拒绝 - Android Permission ACCESS_FINE_LOCATION is always denied Android Fragment中的位置权限 - Location Permission In Android Fragment Android-缩放到用户位置并显示地图 - Android - Zoom to the user location and display the map 缺少 GoogleMap.setMyLocationEnabled 所需的权限:android.permission.ACCESS_COARSE_LOCATION 或 android.permission.ACCESS_FINE_LOCATION - Missing permissions required by GoogleMap.setMyLocationEnabled: android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION Android WifiManager getScanResult抱怨虽然声明了权限,但需要ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限 - Android WifiManager getScanResult complains Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission although declared permission 用户位置未显示在地图上 - User location is not displayed on the map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM