单击位置FAB时不显示位置权限对话框

时间:2018-01-16 09:32:13

标签: android google-maps

我有一张地图片段。在地图片段中,我使用FAB按钮获取用户的当前位置并在地图上显示。我在Manifest文件中声明了以下权限。 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

现在有两种情况:

(1)用户的位置已经开启:在这种情况下,当我按下位置​​FAB按钮时,它会将地图设置为用户当前位置的动画,并在地图上显示蓝点表示用户的当前位置。这是按预期工作的

(2)用户的位置已关闭:现在这是我遇到问题的地方。当用户点击位置FAB时,我想要显示对话框,请求用户打开他的位置&amp;如果授予位置,则移动到用户当前位置。但是,当我按FAB时,没有任何事情发生。以下是位置FAB的setOnClickListener的代码。

locationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("MapFragment", "Location button pressed" );
            try {
                Log.v("MapFragment", "Inside try" );

                if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    Log.v("MapFragment", "inside if 0.7" );
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);
                    Log.v("MapFragment", "inside if 1" );
                    return;
                }
                currentLocation = getLastKnownLocation();
                if (currentLocation != null) {
                    Log.v("MapFragment", "inside if 2" );
                    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), 17);
                    googleMap.animateCamera(cameraUpdate);
                    locationButton.getDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
                }


            } catch (Exception e){
                checkLocationPermission();
                Toast.makeText(getContext(), "Please turn on Location from the Settings", Toast.LENGTH_SHORT).show();

            }
        }
    });

getLastKnownLocation()函数如下:

private Location getLastKnownLocation() {
    //Create a location manager object instance
    LocationManager mLocationManager = (LocationManager) getContext().getSystemService(LOCATION_SERVICE);
    //Get all the different providers which can give current location info
    List<String> providers = mLocationManager.getProviders(true);
    //Initialising the location to be null
    Location bestLocation = null;
    //Creating a for loop to go through all the location providers and get the location
    for (String provider : providers) {

        //if permission is not granted, then get the permission

        if (ContextCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {


            ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        //Get the last known location from the data provider
        Location l = mLocationManager.getLastKnownLocation(provider);
        //If the last know location provided by the data provider is null then ignore the provider and move to the next one.
        if (l == null) {
            continue;
        }

        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            bestLocation = l;
        }
    }
    return bestLocation;
}

我在案例2中按FAB时得到的日志消息如下

01-16 13:52:15.728 18227-18227/in.map.app V/MapFragment: Location button pressed
01-16 13:52:15.728 18227-18227/in.map.app V/MapFragment: Inside try

1 个答案:

答案 0 :(得分:0)

您需要覆盖此方法,并相应地执行下一步操作。

void onRequestPermissionsResult (int requestCode, 
                String[] permissions, 
                int[] grantResults)