谷歌地图显示空白屏幕

6

我正在尝试在一个非常简单的应用程序中显示谷歌地图(一个只有谷歌地图活动的新项目),清单具有以下权限:

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

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<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">


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

<activity
    android:name=".MapsActivity"
    android:label="@string/title_activity_maps">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

    </manifest>

我在 @string/google_maps_key 中拥有有效的密钥。

mainActivity 没有改动,和在 Android Studio 中创建的一样。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

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

同样的情况也发生在布局文件activity_maps中:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ivan.pruebamaps.MapsActivity" />

这是我的Gradle文件,如果有帮助的话:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.ivan.pruebamaps"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.google.android.gms:play-services-maps:10.2.0'
    testCompile 'junit:junit:4.12'
}

当我执行这个应用程序时,屏幕上只出现了一个空白屏幕和Google标志位于左下角。我尝试在模拟的Nexus 5和三星Galaxy S6上运行,但都没有成功。

2
“空白地图”问题通常是由于错误的 API 密钥引起的。请检查日志,里面应该有一个错误。 - Daniel Nugent
实际上,日志中出现了一条消息,告诉我要检查我的密钥是否存在。这太不可思议了,因为我在另一台安装了sdk 24的电脑上尝试了完全相同的项目,它却可以正常工作。我感到非常惊讶。 - Asdemuertes
在我的情况下,它在真实设备上显示正常,但在模拟器上却是一片空白。这并不是API密钥的问题,因为我已经尝试了一个新密钥。我查看了日志,发现有一条消息:"> E/Google Maps Android API: Google Maps Android API v2 only supports devices with OpenGL ES 2.0 and above"。我记得AS之前曾抱怨过我的GPU(Intel HD),所以我尝试在模拟器的设置中更改了几个OpenGL设置,但都没有起作用。也许我需要安装一个专用的GPU或更换CPU。 - Damn Vegetables
6个回答

10

听起来你正在使用在不同机器上生成的API密钥,该机器具有不同的调试密钥库。

您需要将每个开发机器的调试密钥库的SHA1指纹添加到开发人员控制台中的API密钥中。

此外,一旦您准备好进行签名发布构建,请确保在google_maps_api.xml文件的发布版本中使用您的发布密钥库的SHA1指纹的API密钥。更多信息请参见此处


1

我已经在s6终端中安装了它。在模拟器中尝试启动应用程序时,我没有遇到任何错误。 - Asdemuertes

1
在我的情况下,问题应该是API密钥。当我用新生成的API密钥替换旧的时,地图就恢复了在线状态。

1
这个问题是由于API密钥引起的。
步骤:
  1. 进入您的Google API控制台。
  2. 导航到凭据。
  3. 查找Android密钥(由Google服务自动创建)。
  4. 在您的manifest.xml中使用此密钥


0

如果你在应用程序中使用AVD(Android虚拟设备),有些情况下它可能无法正常工作。

我建议使用真实设备测试,这对我来说很有效。

我在主活动中扩展了AppCompatActivity并使用了Google地图片段,它可以正常工作!


0

我曾经遇到过同样的问题,通过在我正在调试的设备上完全卸载应用程序,然后再重新安装解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接