【问题标题】:google maps showing blank screen谷歌地图显示黑屏
【发布时间】:2017-03-24 23:40:57
【问题描述】:

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

<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中,两者都不起作用。

【问题讨论】:

  • “空白地图”问题始终是由错误的 API 密钥引起的。检查日志,那里应该有错误。
  • 确实,我在日志中收到一条消息,告诉我检查我的密钥是否存在。这太不可思议了,因为我在另一台带有 sdk 24 的 PC 上尝试了完全相同的项目并且它工作正常。我很震惊。
  • 在我的例子中,它显示在真实设备上,但在模拟器上是空白的。这不是 API 密钥问题,因为我尝试了新密钥。我查看了日志,有一条消息,> E/Google Maps Android API: Google Maps Android API v2 only support > devices with OpenGL ES 2.0 and above 我记得AS之前抱怨过我的GPU(Intel HD),所以我在模拟器的设置中尝试了几个 OpenGL 设置,但都没有奏效。也许我必须安装专用 GPU 或更换 CPU。

标签: android google-maps


【解决方案1】:

听起来您使用的 API 密钥是在另一台机器上生成的,该机器具有不同的调试密钥库。

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

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

【讨论】:

    【解决方案2】:

    尝试通过android sdk安装google play服务再试一次。

    这可能有助于How to download Google Play Services in an Android emulator?

    【讨论】:

    • 我已经将它安装在 s6 终端中。在模拟器中我尝试启动应用程序时没有任何错误。
    【解决方案3】:

    就我而言,问题出在我猜的 API 密钥上。一旦我用生成的新 API 密钥更改了我的 API 密钥,地图就会重新上线。

    【讨论】:

      【解决方案4】:

      这个问题是因为 API 密钥。

      步骤:

      1. 转到您的 Google API 仪表板。
      2. 导航到凭据,
      3. 查找 Android 密钥(由 Google 服务自动创建)。
      4. 在你的 manifest.xml 中使用这个键

      【讨论】:

        【解决方案5】:

        如果您为您的应用使用 avd(Android 虚拟设备),在某些情况下,它将无法正常工作

        使用适合我的真实设备

        我在主要活动中使用 googlemap 片段扩展了 appcompatactivity 它有效!

        【讨论】:

          【解决方案6】:

          遇到了同样的问题,通过从我正在调试的设备上完全卸载该应用程序,然后再次安装它来解决它。

          【讨论】:

            猜你喜欢
            • 2018-04-03
            • 2017-03-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多