缺少 FusedLocationProviderClient.getLastLocation 所需的权限:android.permission.ACCESS_COARSE_LOCATION

Posted

技术标签:

【中文标题】缺少 FusedLocationProviderClient.getLastLocation 所需的权限:android.permission.ACCESS_COARSE_LOCATION【英文标题】:Missing permissions required by FusedLocationProviderClient.getLastLocation: android.permission.ACCESS_COARSE_LOCATION 【发布时间】:2020-10-13 09:56:54 【问题描述】:

mFusedLocationProviderClient.getLastLocation().addOnCompleteListener 和 mMap.setMyLocationEnabled(true) 这两行都给了我我在标题中提到的错误。我不知道是什么问题..我看了很多视频。但是找不到解决方案警告或说错误,因为有时它的工作或有时应用程序会崩溃

ShoOwnerMaps

package com.example.saloononlinewaitingsolution;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.GeoPoint;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.maps.android.clustering.ClusterManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ShoOwnerMaps extends AppCompatActivity implements OnMapReadyCallback 


    public static GeoPoint gp;
    private static final String TAG = "ShoOwnerMaps";
    private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    private static final String COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
    private Boolean mLocationPermissionGranted = false;
    private FusedLocationProviderClient mFusedLocationProviderClient;
    private static final float DEFAULT_ZOOM = 15f;
    private GoogleMap mMap;
    private ImageView mSearchLocation;
    private FirebaseFirestore mdb;
    //CollectionReference location;
    private GeoPoint cGeoPoint;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sho_owner_maps);

        mSearchLocation = (ImageView) findViewById(R.id.ic_searchLocation);
        mdb = FirebaseFirestore.getInstance();
        // location = mdb.collection("Saloons Shop Location");
        //mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
      mFusedLocationProviderClient = new FusedLocationProviderClient(this);
        getLocationPermission();



    

    private void getLocationPermission() 
        Log.d(TAG, "getLocationPermission: getting location permission ");
        String[] permission = Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION;

        if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
                FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) 
            if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
                    COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED) 
                mLocationPermissionGranted = true;
                initializeMap();
             else 
                ActivityCompat.requestPermissions(this, permission, LOCATION_PERMISSION_REQUEST_CODE);
            
         else 
            ActivityCompat.requestPermissions(this, permission, LOCATION_PERMISSION_REQUEST_CODE);
        

    

 /*   @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) 
        Log.d(TAG, "onRequestPermissionsResult: called ");
        mLocationPermissionGranted = false;
        switch (requestCode) 
            case LOCATION_PERMISSION_REQUEST_CODE: 
                if (grantResults.length > 0) 
                    for (int i = 0; i < grantResults.length; i++) 
                        if (grantResults[i] != PackageManager.PERMISSION_GRANTED) 
                            mLocationPermissionGranted = false;
                            Log.d(TAG, "onRequestPermissionsResult: permission Failed");
                            return;
                        
                    
                    Log.d(TAG, "onRequestPermissionsResult: permission Granted");
                    mLocationPermissionGranted = true;
                    initializeMap();

                
            
        
    */


    private void initializeMap() 
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(ShoOwnerMaps.this);

    

    @Override
    public void onMapReady(GoogleMap googleMap) 
        Toast.makeText(this, "Map is Ready", Toast.LENGTH_SHORT).show();

        Log.d(TAG, "initMap : initializing map");
        mMap = googleMap;

        if (mLocationPermissionGranted) 
            // getDeviceLocation();
            lastKnownLocation();

            if (ContextCompat.checkSelfPermission(this,
                    android.Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED )
            
                ActivityCompat.requestPermissions(this,new String[]
                                Manifest.permission.ACCESS_FINE_LOCATION,
                        LOCATION_PERMISSION_REQUEST_CODE);
                mMap.setMyLocationEnabled(true);

               return;
            
;

            mMap.getUiSettings().setMyLocationButtonEnabled(false);
            mMap.getUiSettings().setCompassEnabled(true);
            mMap.getUiSettings().setZoomControlsEnabled(true);
            mMap.getUiSettings().setTiltGesturesEnabled(true);
            mMap.getUiSettings().setRotateGesturesEnabled(true);
            mMap.setTrafficEnabled(true);


        
    



    private void lastKnownLocation() 
        Log.d(TAG, "lastKnownLocation: called");

        if (ContextCompat.checkSelfPermission(this,
             Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED )
        

            ActivityCompat.requestPermissions(this,new String[]
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    LOCATION_PERMISSION_REQUEST_CODE);

        

        mFusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() 
            @Override
            public void onComplete(@NonNull Task<Location> task) 

                if (task.isSuccessful()) 


                    Location currentLocation = task.getResult();
                    assert currentLocation != null;
                    final GeoPoint currentGeoPoint = new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude());
                    moveCamera(new LatLng(currentGeoPoint.getLatitude(), currentGeoPoint.getLongitude()), DEFAULT_ZOOM,
                            "My Location");
                    cGeoPoint = currentGeoPoint;

                    mSearchLocation.setOnClickListener(new View.OnClickListener() 
                        @Override
                        public void onClick(View view) 

                            gp = cGeoPoint;
                            Intent intent = new Intent(ShoOwnerMaps.this, ShopTimeDaySchedule.class);
                            startActivity(intent);
                            finish();
                        
                    );

                
            
        );
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) 
        Log.d(TAG, "onRequestPermissionsResult: called ");
        mLocationPermissionGranted = false;
        switch (requestCode) 
            case LOCATION_PERMISSION_REQUEST_CODE: 
                if (grantResults.length > 0) 
                    for (int i = 0; i < grantResults.length; i++) 
                        if (grantResults[i] != PackageManager.PERMISSION_GRANTED) 
                            mLocationPermissionGranted = false;
                            Log.d(TAG, "onRequestPermissionsResult: permission Failed");
                            return;
                        
                    
                    Log.d(TAG, "onRequestPermissionsResult: permission Granted");
                    mLocationPermissionGranted = true;
                    initializeMap();

                
            
        
    


    private void moveCamera(LatLng latLng, float zoom, String title) 
        Log.d(TAG, "moveCamera: moving the camera to Lat " + latLng.latitude + ", lng :" + latLng.longitude);

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));

        MarkerOptions options = new MarkerOptions()
                .position(latLng)
                .title(title);
        mMap.addMarker(options);

    

build.gradle

悬停时发出警告

清单

【问题讨论】:

【参考方案1】:

在调用mFusedLocationProviderClient.getLastLocation()之前检查ACCESS_COARSE_LOCATION、ACCESS_FINE_LOCATION权限是否被授予,如下图:

if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
    FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) 
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
        COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED) 
    mFusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener < Location > () 
        //override methods
    );
 else 
    ActivityCompat.requestPermissions(this, permission, LOCATION_PERMISSION_REQUEST_CODE);

 else 
  ActivityCompat.requestPermissions(this, permission, 
LOCATION_PERMISSION_REQUEST_CODE);
 

【讨论】:

我试过了,但 android studio 仍然告诉我:“缺少 FusedLocationProviderClient.requestLocationUpdates 所需的权限:android.permission.ACCESS_COARSE_LOCATION 或 android.permission.ACCESS_FINE_LOCATION” @user15155006 即使添加了checkSelfPermission() 警卫,我也一直收到警告。我的解决方案是编辑 AndroidManifest.xml。这似乎触发了正确的更新以使其消失。【参考方案2】:

“COURSE_LOCATION” - 这是错字^^'

【讨论】:

以上是关于缺少 FusedLocationProviderClient.getLastLocation 所需的权限:android.permission.ACCESS_COARSE_LOCATION的主要内容,如果未能解决你的问题,请参考以下文章

调用支付jsapi 缺少参数appid

缺少 RapidAPI 应用程序密钥(不缺少)

调用jsapi缺少参数$key0$

SQL 错误:ORA-00907:缺少右括号 00907。00000 -“缺少右括号”

Kotlin 的列表缺少“添加”、“删除”、地图缺少“放置”等?

PLSQL Developer登陆的时候缺少一个选择连接为的选择框