Api 23 以上未显示当前位置

Current Location is not display above Api 23

我在 API 23 之前使用地图视图显示地图我的应用程序工作正常并显示当前位置但是当我 运行 在 23 及以上时它不显示当前 location.My代码如下:

 mMap = googleMap;
        mMap.getUiSettings().setMapToolbarEnabled(false);

        LatLng loc = new LatLng(currentLatitude, currentLongitude);
        //mMap.addMarker(new MarkerOptions().position(loc).title(currentLocation + ", none"));
        //mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 14.0f));
        if (ActivityCompat.checkSelfPermission(MapClass.this.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MapClass.this.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mMap.setMyLocationEnabled(true);

帮帮我?

查看有关请求的文档Permissions at Run Time 指出:

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app's Settings screen.

If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
            @NonNull int[] grantResults) {
        if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
            return;
        }

        if (PermissionUtils.isPermissionGranted(permissions, grantResults,
                Manifest.permission.ACCESS_FINE_LOCATION)) {
            // Enable the my location layer if the permission has been granted.
            enableMyLocation();
        } else {
            // Display the missing permission error dialog when the fragments resume.
            mPermissionDenied = true;
        }
    }

GitHub 上的 ApiDemos repository 包括演示在地图上使用位置的示例:

希望对您有所帮助。

我可能来晚了一点,但这个答案将帮助那些遇到同样问题并希望 运行 他们的应用程序在两个版本上的人。问题是 API 大于 21,您需要请求 Run time permission

首先,我创建了一个名为 gettingLocationBasedOnApiVersion(mMap) 的函数,其中放置了我使用用户位置的所有代码。但事先,在 onMapReady() 中我检查了 API 版本,如果 API 版本超过 21,我请求权限。

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if(Build.VERSION.SDK_INT <= 21){
            gettingLocationBasedOnApiVersion(mMap);
            Toast.makeText(getApplicationContext(), "APK less than 21", Toast.LENGTH_LONG).show();
        }else{
            Log.i("SDK Version", "SDK more than 21 will require permissions on run time");
            Toast.makeText(getApplicationContext(), "APK greater than 21", Toast.LENGTH_LONG).show();
            enableMyLocation();
        }
    }

然后在启用函数中我要求 运行 时间权限。

private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private boolean mPermissionDenied = false;
private void enableMyLocation() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            // Permission to access the location is missing.
            PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
                    Manifest.permission.ACCESS_FINE_LOCATION, true);
        } else if (mMap != null) {
            // Access to the location has been granted to the app.
            mMap.setMyLocationEnabled(true);
            gettingLocationBasedOnApiVersion(mMap);

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
            return;
        }

        if (PermissionUtils.isPermissionGranted(permissions, grantResults,
                Manifest.permission.ACCESS_FINE_LOCATION)) {
            // Enable the my location layer if the permission has been granted.
            enableMyLocation();
        } else {
            // Display the missing permission error dialog when the fragments resume.
            mPermissionDenied = true;
        }
    }

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        if (mPermissionDenied) {
            // Permission was not granted, display error dialog.
            showMissingPermissionError();
            mPermissionDenied = false;
        }
    }

    /**
     * Displays a dialog with error message explaining that the location permission is missing.
     */
    private void showMissingPermissionError() {
        PermissionUtils.PermissionDeniedDialog
                .newInstance(true).show(getSupportFragmentManager(), "dialog");
    }

为了更好的理解,使用你可以参考我的github页面上的Places Class。 PS。不要忘记在您的 app 文件夹中添加 PermissionUtils Class。