谷歌地图V2在4.4上显示空白屏幕,左下角显示谷歌徽标

时间:2015-01-28 23:56:10

标签: android google-maps

我在这里绝对疯狂,我经历过这么多帖子,似乎没有任何工作。我的设备肯定是连接到播放服务,因为我检查过,当我查看我的api凭据时,我的密钥显示我的应用程序。由于某些未知的原因,我似乎无法显示地图,只是空白屏幕,我花了3个小时就没有运气。任何帮助,为什么我不能让地图显示将非常感谢!我甚至试过没有运气重新生成钥匙。

AndroidMainifest.xml

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

<permission
    android:name="com.*****.****.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.*****.****.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="********-***********"/>

</application>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<fragment
    android:id="@+id/mapView"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

MainActivity.java

package com.zoome.natalie.zoo;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;


public class MainActivity extends Activity {

private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
GoogleMap googleMap;
// tag used on log messages.
static final String TAG = "HelloGooglePlayServices";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    createMap();
}

@Override
protected void onResume() {
    //auto-generated method stub
    super.onResume();

    // You need to do the Play Services APK check here too.
    checkPlayServices();

}

private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        } else {
            Log.i(TAG, "Device not supported.");
            finish();
        }
        return false;
    }
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void createMap() {
    try {
        if (null == googleMap) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.mapView)).getMap();

            /**
             * If the map is still null after attempted initialisation,
             * show an error to the user
             */
            if (null == googleMap) {
                Toast.makeText(getApplicationContext(),
                        "Error creating map", Toast.LENGTH_SHORT).show();
            }
        }
    } catch (NullPointerException exception) {
        Log.e("mapApp", exception.toString());
    }
}
}

如果它对build.gradle模块有用

apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "com.zoome.natalie.zoo"
    minSdkVersion 15
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'

compile 'com.google.android.gms:play-services-maps:6.5.87'
}

调试密钥是我最不确定的,但我认为这是正确的,任何有关尝试或我犯错误的帮助都会非常感激。感谢。

4 个答案:

答案 0 :(得分:4)

假设您安装了SDK内容(我相信您基于使用Google徽标进入灰色屏幕的事实),您要做的第一件事就是创建调试和发布密钥库。完整的文档在这里:

https://developers.google.com/maps/documentation/android/start

这是Mac / Linux用户的快速和肮脏。在终端中键入以下内容:

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

然后,您应该看到要用于调试版本的SHA1哈希。复制并粘贴到某处。

我不确定你是否正在尝试编译此应用程序以进行发布,但现在让我们完成这些动作并为您节省头痛的麻烦。在Android Studio中,点击构建 - &gt;生成签名APK

这将引导您完成一个小小的演练过程,您可以在其中选择要编译的模块,然后选择要用于模块的密钥。此时,您需要点击新建

这将让您决定保存发布密钥的位置,设置密码以及当时可以设置的其他一些选项。

现在获取发行密钥的SHA1哈希,使用与上面相同的keytool命令,但替换刚刚创建的文件的路径和用于密码的密码参数

现在转到

https://console.developers.google.com/project

选择您的项目,然后转到左侧的凭据链接。如果您还没有,请将自己设为Android的公共API访问密钥。如果您有,请单击编辑允许的Android应用程序按钮。粘贴之前记下的两个SHA1键,然后在每行后面加上'; your.package.name'。现在,将API KEY粘贴到某处。

返回Android Studio,在调试和发布文件夹中找到google_maps_api.xml文件。路径应为:

包/应用/ SRC /调试/ RES /值/ google_maps_api.xml 包/应用程序/ SRC /释放/ RES /值/ google_maps_api.xml

打开每一个并粘贴在这样的strink中:

<string name="google_maps_key" templateMergeStrategy="preserve">API KEY</string>

最后,进入你的AndroidManifest并说出来:

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

拿啤酒庆祝

答案 1 :(得分:0)

如果在地图的视图区域看到空白屏幕(或网格),可能性是,您正在使用版本1的API密钥并将其放入清单中。请查看您的开发人员控制台并查找Google Maps V2密钥!

答案 2 :(得分:0)

如果您看到一个没有任何网格的空白屏幕,左下角有一个Google徽标,那么您创建的API密钥很可能不是 合适的一个,您不应该创建一个简单的API密钥,而是在Google Developers Console中为Google Maps Android API注册您的应用程序的密钥。正确的链接会引导您进入&#34;启用API页面&#34;,此处为链接 https://console.developers.google.com/flows/enableapiapiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&reusekey=true

你的清单和布局对我来说都很好看,MainActivity没有什么不寻常的......

答案 3 :(得分:0)

请正确创建api密钥;以下链接提供了所需的详细信息:

https://developers.google.com/maps/documentation/android-api/signup

在运行应用程序之前,请确保您已在手机上启用互联网。