【问题标题】:Adding Default Location if User Doesn't Allow Permission for a Fine Location for a Map in Android如果用户不允许在 Android 中使用地图的精细位置权限,则添加默认位置
【发布时间】:2017-12-17 12:57:01
【问题描述】:

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

我的地图片段:

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

}

【问题讨论】:

    标签: java android google-maps google-maps-android-api-2 runtime-permissions


    【解决方案1】:

    您可以使用此代码手动缩放到位置:

    CameraUpdate center=CameraUpdateFactory.newLatLng(new LatLng(latitude,
    longitude));
    
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
    
    map.moveCamera(center);
    map.animateCamera(zoom);
    

    【讨论】:

    • 最后一个 IF 条件之后
    • 我已经更新了我的代码,这已经存在于我的代码中。我想要的是当用户点击不允许权限时,它应该转到自定义位置。
    • 可能是如果用户没有授予权限而不是从第一个 if 条件返回而不执行剩余代码。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-10-06
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多