Google Maps Android API:授权失败

问题描述 投票:0回答:1

Android Studio 3.6

我在我的app / build.gradle文件中添加了以下内容:

implementation 'com.google.android.gms:play-services-maps:17.0.0'

google_maps_api.xml中,我这样添加了我的API密钥:

    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyAu_1111111</string>

在清单文件中:

  <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAu_1111111" />

在Google控制台上,我的API密钥已启用

这是我的布局:

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/profileDetailsToolbar"
tools:context=".ui.activity.MapActivity" />

这是我的活动:

class MapActivity : RootActivity(), OnMapReadyCallback {

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinding =
            DataBindingUtil.setContentView(this, R.layout.map_activity)
        dataBinding.setHandler(this)


        init()
    }

    private fun init() {
        setSupportActionBar(findViewById(R.id.toolBar))
        getSupportActionBar()?.setDisplayHomeAsUpEnabled(true)
        getSupportActionBar()?.setHomeButtonEnabled(true)
        getSupportActionBar()?.setDisplayShowTitleEnabled(false)

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }
    override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap

        // Add a marker in Sydney and move the camera
        val sydney = LatLng(-34.0, 151.0)
        mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
    }
}

但是当我开始活动时,地图不会显示。在logcat中,我得到下一个错误:

12-20 09:59:10.675 E/Google Maps Android API( 5153): Authorization failure.  Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.
12-20 09:59:10.676 E/Google Maps Android API( 5153): In the Google Developer Console (https://console.developers.google.com)
12-20 09:59:10.676 E/Google Maps Android API( 5153): Ensure that the "Google Maps Android API v2" is enabled.
12-20 09:59:10.676 E/Google Maps Android API( 5153): Ensure that the following Android Key exists:
12-20 09:59:10.676 E/Google Maps Android API( 5153):    API Key: YOUR_API_KEY
12-20 09:59:10.676 E/Google Maps Android API( 5153):    Android Application (<cert_fingerprint>;<package_name>): some_hex_value;com.myproject
android-studio-3.0 google-maps-api-2
1个回答
0
投票

由于您已经在使用API​​密钥,所以该错误可能是由以下任何原因引起的:

  • 使用API​​密钥,但来自没有有效计费帐户的项目
  • 使用无效/不正确/格式错误的API密钥
  • 使用除您自己以外的其他应用程序所用的API密钥
  • 您的项目未启用Maps SDK for Android

请确保您正在使用具有有效计费帐户的项目中的API密钥,因为Maps SDK for Android documentation中已说明,您必须先注册并创建一个计费帐户,然后才能开始使用Google Maps Platform API和SDK。 。

此外,如果您的API密钥有限制,请确保根据API key best Practices对其进行了适当限制。最后,请确保您已在GCP console中为地图项目启用了Android Maps SDK。

© www.soinside.com 2019 - 2024. All rights reserved.