android - 谷歌地图显示空白屏幕

标签 android google-maps

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

<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 中有一个有效的 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 Logo 。我在模拟的 Nexus 5 中尝试,在三星银河 s6 中,两者都不起作用。

最佳答案

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

您需要将您使用的每台开发机器的调试 keystore 的 SHA1 指纹添加到开发人员控制台中的 API key 。

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

关于android - 谷歌地图显示空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43010837/

相关文章:

android - 在线播放多首音频

android - 如何使用 GSON 模型处理 Android 中的 OrmLite ForeignCollection 字段

android - Android应用添加MapView时出现cross-loader异常

java - 包 com.google.android.gms.samples.wallet 不存在导入 com.google.android.gms.samples.wallet.Constants

android - 移动设备上的图像处理速度

android - 后台套接字连接

ios - Google Places Autocomplete,按机构/餐馆过滤

javascript - 多次触发谷歌地图 get_bounds 事件

android - Google Maps V2 连接问题

Javascript:谷歌地图标记列表中的 href 错误