【问题标题】:Google Maps API Intergration Call requires permissionGoogle Maps API 集成调用需要权限
【发布时间】:2018-04-12 00:12:42
【问题描述】:

我想在我的 Android Studio 项目中实现我的位置按钮。

如果我在我的应用程序中添加代码mMap.setMyLocationEnabled(true);,Android Studio 会说:

(调用需要权限,可能会被用户拒绝:代码应显式检查权限是否可用(使用 checkPermission)或显式处理潜在的 SecurityExceptio...)

package com.example.simon.myapplication1;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

    private TextView mTextMessage;
    private GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

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

        // Add a marker in Sydney and move the camera
        LatLng aachen = new LatLng(50.774796, 6.088965);
        mMap.addMarker(new MarkerOptions().position(aachen).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(aachen));
        mMap.setMyLocationEnabled(true);

    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }
    };




}

感谢您的帮助!

【问题讨论】:

  • 您是否在应用中获得了用户的许可? (您可以通过在清单中添加权限以及ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 来做到这一点)

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


【解决方案1】:

如果你想在你的应用中使用用户位置,你必须在 AndroidManifest 中添加位置权限

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simon.myapplication1" >
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>

因为直到Android 6.0 Location 都属于“危险权限”,你必须添加代码来检查权限是否被授予:

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_COARSE_LOCATION)
    != PackageManager.PERMISSION_GRANTED) {
    // Permission is not granted
}

如果应用没有授予位置权限,则需要向用户询问

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

// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.ACCESS_COARSE_LOCATION)) {

    // Show an explanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.

} else {

    // No explanation needed; request the permission
    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
            MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);

    // MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}
} else {
// Permission has already been granted
}

并处理结果:

@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
switch (requestCode) {
    case MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // permission was granted, yay! Do the
            // contacts-related task you need to do.
        } else {
            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
        return;
    }

    // other 'case' lines to check for other
    // permissions this app might request.
}
}

Request code is from here

Read more about dangerous permissions politics

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多