简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM