Google Maps API 集成调用需要许可

奥胡尔

我想将我的位置按钮实现到我的 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;
        }
    };




}

感谢帮助!

粘土

如果你想在你的应用中使用用户位置,你必须在 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.
}
}

请求代码来自这里

阅读有关危险权限策略的更多信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google Maps Api许可

来自分类Dev

需要使用Google Maps API的帮助

来自分类Dev

向Google Maps API调用添加承诺

来自分类Dev

调用Google Maps API时遇到COORS

来自分类Dev

使用Retrofit GET调用的Google Maps API

来自分类Dev

要使用Google Maps API,需要哪个API密钥?

来自分类Dev

Google Maps API RefererNotAllowedError

来自分类Dev

Google Maps API DeletedApiProjectMapError

来自分类Dev

Google Maps Api

来自分类Dev

Google Maps REST API

来自分类Dev

Google Maps API请求

来自分类Dev

Google Maps Directions API

来自分类Dev

Google Maps API参数

来自分类Dev

Android Studio集成了Google Maps API v2

来自分类Dev

Google Maps Places:为什么需要API密钥?

来自分类Dev

Google Maps Places:为什么需要API密钥?

来自分类Dev

Clojurescript:调用Google Maps API时出现问题

来自分类Dev

什么构成对基本Google Maps JS API的调用?

来自分类Dev

Google Maps-直接从javascript调用API或使用SDK

来自分类Dev

如何在for循环中处理Google Maps API调用

来自分类Dev

Google Maps API v2对getmap()的调用引发NullPointerException

来自分类Dev

使用dart:js调用Google Maps API事件addListener

来自分类Dev

用ajax调用Google Maps Api不起作用

来自分类Dev

通过其他API调用的Google Maps Info Window

来自分类Dev

Google Maps Javascript API与Google Maps Embed API

来自分类Dev

缺少Google Maps RouteBoxer API

来自分类Dev

异步加载Google Maps API

来自分类Dev

动态加载Google Maps API

来自分类Dev

Google Maps API的JavaScript框架