【问题标题】:API: Authorization failure Google Maps (blank screen)API:授权失败谷歌地图(空白屏幕)
【发布时间】:2018-06-21 08:24:10
【问题描述】:

我在一个 Activity 中有一个 GoogleMaps 片段,但我根本无法让它工作。每次打开它,屏幕都是空白的,只有“设置我的位置”图标。地图永远不会出现。

当我查看 LogCat 时,我明白了:

01-11 22:53:22.984 29341-30537/package_name
E/Google Maps Android API: Authorization failure.  Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.
01-11 22:53:23.004 29341-30537/package_name E/Google Maps Android API: In the Google Developer Console (https://console.developers.google.com)
Ensure that the "Google Maps Android API v2" is enabled.

这是一个与 API 相关联的明确标志,因此我尝试了所有可能的方法来使该密钥工作。这些是我尝试过的选项:

  1. 创建没有任何限制的 API 代码。没用!
  2. 创建对我的应用程序有限制的 API 代码。我使用了 google_maps.api.xml 文件和 Gradle 签名报告给出的包名称和代码。同样的错误仍然存​​在。我什至添加了多个对应于调试和发布的 SHA-1 证书(来自 Google Play 证书)。
  3. 正在搜索要启用的 Google Maps Android API v2。我的控制台中没有这样的东西。唯一的包(Google Maps Android API)已启用。

我浪费了我在互联网上可以找到的所有可能选项。谁能帮我解决这个问题?

我现在能看到的唯一可疑行为是 google_maps.api.xml 文件有一个(调试)扩展名,用灰色写成。我不知道这是否与此错误有关。我是这样看的:google_maps.api.xml (debug) 在 Android Studio 中。

< manifest xmlns: android = "http://schemas.android.com/apk/res/android"
package = "package" >

  <
  uses - permission android: name = "android.permission.INTERNET" / >
  <
  uses - permission android: name = "android.permission.CALL_PHONE" / >
  <
  uses - permission android: name = "android.permission.VIBRATE" / >
  <
  uses - permission android: name = "android.permission.WRITE_EXTERNAL_STORAGE" / >
  <
  uses - permission android: name = "android.permission.CAMERA" / >
  <
  uses - permission android: name = "android.permission.ACCESS_FINE_LOCATION" / >
  <
  uses - permission android: name = "android.permission.ACCESS_COARSE_LOCATION" / >


  <
  application
android: allowBackup = "true"
android: icon = "@mipmap/app_logo"
android: label = "@string/app_name"
android: roundIcon = "@mipmap/app_logo"
android: supportsRtl = "true"
android: theme = "@style/AppTheme" >
  <
  activity android: name = ".MainScreenActivity" >
  <
  intent - filter >
  <
  action android: name = "android.intent.action.MAIN" / >

  <
  category android: name = "android.intent.category.LAUNCHER" / >
  <
  /intent-filter> <
  /activity> <
  activity android: name = ".MainMenu" / >

  //And some other activities

  <
  meta - data
android: name = "com.google.android.geo.API_KEY"
android: value = "@string/google_maps_key" / >

  <
  activity
android: name = ".Menu"
android: theme = "@style/AppTheme" / >
  //And some other activities
  <
  /application>

  <
  /manifest>

【问题讨论】:

  • “@string/google_maps_key”有效吗?确保您在字符串资源中放置了有效的 google_maps_key。检查是否有任何其他必需的密钥/令牌/秘密也必须添加到字符串资源文件中。
  • 谢谢!你的建议有帮助。请参阅下面的答案。

标签: android api google-maps android-studio


【解决方案1】:

我已经通过使用 Shn_Android_Dev 在评论中给出的建议解决了这个问题(谢谢)。显然它与(调试)文件有关。

我总是将 API 代码放在 google_maps_api.xml (debug) 中,在清单文件中也给出了 android:value="@string/google_maps_key"。但是,出于某种原因,当我打开应用程序时,它告诉我 API 不正确。 Logcat 中显示为错误的错误 API 实际上是位于名为 strings.xml (debug) 的文件中的 API。所以我在两个不同的文件中有两个不同的 API。请看下图。

正如我所怀疑的,秘密还在于strings.xml (debug)google_maps_api.xml (debug)。一直在调试,我在那里设置了 API,但是当生成签名的 APK 到 GooglePlay 时,AS 在后台更改为 strings.xml (release)google_maps.xml (release)。但是,只有将 Built Variants 更改为 Release 时,才能访问 release 部分。否则,需要在 release\values 文件夹中手动更改 API 密钥。

问题现在解决了。这些地图现在也显示在 Google Play 上。我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 2013-05-16
    相关资源
    最近更新 更多