API:授权失败谷歌地图(空白屏幕)

时间:2018-01-11 22:14:44

标签: android api google-maps android-studio

我在活动中有一个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代码(Check link here)。它没有用!
  2. 创建限制我的应用的API代码。我使用了包名称和google_maps.api.xml文件以及Gradle签名报告给出的代码。同样的错误仍然存​​在。我甚至添加了多个与调试和发布相对应的SHA-1证书(来自Google Play证书)。
  3. 搜索Google Maps Android API v2以启用。在我的控制台中没有这样的东西。唯一的包(Google Maps Android API)已启用。 (See screenshot here)
  4. 我浪费了所有可以在互联网上找到的选项。任何人都可以帮我解决这个问题吗?

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

    
    
    < 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>
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:1)

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

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

enter image description here

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

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