【问题标题】:Google Maps API displaying grey screen instead of map view on AndroidGoogle Maps API 在 Android 上显示灰屏而不是地图视图
【发布时间】:2019-01-03 00:00:51
【问题描述】:

我正在尝试在 Android 上显示 Google 地图。当我加载活动时,我得到一个灰屏,左下角显示谷歌徽标。使用虚拟模拟器和真实设备也会发生这种情况。我尝试使用不同的 Api 密钥,但遇到了同样的问题。我正在使用 Android Studio 提供的 Map 活动。

build.gradle(模块:app)

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28
    defaultConfig {

        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-auth:16.1.0'
    implementation 'com.google.firebase:firebase-database:16.0.1'
    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    implementation 'com.google.android.gms:play-services-location:16.0.0'

}

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="">

    <uses-feature
        android:name="android.hardware.sensor.stepcounter"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.sensor.stepdetector"
        android:required="true" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gfs.permission.READ_GSERVICES" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".Activities.LandingActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

        <activity
            android:name=".Activities.MapsActivity"
            android:label="@string/title_activity_maps" />
    </application>

</manifest>

错误模式下的Logcat

01-02 15:06:24.913 17090-17160/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-02 15:06:24.923 17090-17160/ 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.
    Ensure that the following Android Key exists:
        API Key: ***
        Android Application (<cert_fingerprint>;<package_name>): ***;

【问题讨论】:

  • 发布日志
  • 尝试了这个解决方案,但它并没有解决我的问题。
  • 确保在MapView 上致电onCreate()onResume()
  • @user10431501 添加logcat 输出。否则这只是猜测。如果您有 targetSdkVersion 28 而没有旧版支持,正如重复问题所暗示的那样,它肯定会在 API 28 模拟器上崩溃,而不是在 API 27 模拟器上。

标签: android google-maps


【解决方案1】:

转到Google console 并尝试为Android 启用地图sdk...

【讨论】:

  • 我已经拿到了3次新钥匙。
  • 正确的密钥是您从 google 控制台获得的密钥
  • 我去控制台把它改成了这个键,同样的问题。
  • 并且设置为启用。
  • 我已经附上了截图。
【解决方案2】:

尝试将硬件传感器更改为不需要:

<uses-feature
    android:name="android.hardware.sensor.stepcounter"
    android:required="false"/>

<uses-feature
    android:name="android.hardware.sensor.stepdetector"
    android:required="false"/>

多年来也不再需要此许可:

<uses-permission android:name="com.google.android.providers.gfs.permission.READ_GSERVICES" />

问题实际上源于:

apply plugin: "com.google.gms.google-services"

必须将其移至文件的最底部。

【讨论】:

  • 它们都与项目的不同部分有关,但是我确实尝试将它们设置为 false 并没有解决问题。
  • 我已将此行移至最底部,但同样的问题。
  • @user10431501 然后您必须按字面意思获取logcat 输出,并且 a) 检查是否确实使用了正确的 API 密钥(同时检查是否设置了可能的 API 限制)以及 Maps API已启用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
相关资源
最近更新 更多